在非SqlServer数据库上实现MemberShip和Role功能(自定义MemberShipProvider和RoleProvider)

简介: 默认情况下,.Net网站上的MemberShip和Role功能只能用于SqlServer,如果要在其它数据库,比如Access,Oracle上使用该功能该怎么办呢? 其实MS早就为我们考虑到了,用户只要从MemberShipProvider和RoleProvider派生自己的Provider类,并...
默认情况下,.Net网站上的MemberShip和Role功能只能用于SqlServer,如果要在其它数据库,比如Access,Oracle上使用该功能该怎么办呢?

其实MS早就为我们考虑到了,用户只要从MemberShipProvider和RoleProvider派生自己的Provider类,并实现相关的方法和属性就可以了,其实ASPX中的MemberShip功能就是这二个抽象类在SqlServer上的实现(有兴趣的朋友可以查阅一下 System.Web.Security.SqlMembershipProvider)

这里只是给出一个MemberShip的演示,数据库结构如下:
用户表T_LoginUser
F_ID            用户ID
F_LoginName     登录名  
F_Password      登录密码

自定义一个MyMemberShipProvider类,这里只实现了三个方法( Initialize, ValidateUser, CreateUser)

using  System;
using  System.Data;
using  System.Data.SqlClient;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

/**/ /// <summary>
/// MyMemberShipProvider 的摘要说明
/// </summary>

public   class  MyMemberShipProvider:System.Web.Security.MembershipProvider
{
    
public MyMemberShipProvider()
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }



    
private string connStr;
    
private bool _requiresQuestionAndAnswer;
    
private int _minRequiredPasswordLength;

    
public override int MinRequiredPasswordLength
    
{
        
get return _minRequiredPasswordLength; }
    }

    
public override bool RequiresQuestionAndAnswer
    
{
        
get
        
{
            
return _requiresQuestionAndAnswer;
        }

    }


    
/**//// <summary>
    
/// 初始化
    
/// </summary>
    
/// <param name="name"></param>
    
/// <param name="config"></param>

    public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
    
{
        
if (config["requiresQuestionAndAnswer"].ToLower() == "true")
        
{
            _requiresQuestionAndAnswer 
= true;
        }

        
else
        
{
            _requiresQuestionAndAnswer 
= false;
        }

        
int.TryParse (config["minRequiredPasswordLength"],out _minRequiredPasswordLength );
        connStr 
= config["connectionString"];
        
base.Initialize(name, config);
    }


    
/**//// <summary>
    
/// 验证用户
    
/// </summary>
    
/// <param name="username"></param>
    
/// <param name="password"></param>
    
/// <returns></returns>

    public override bool ValidateUser(string username, string password)
    
{
        
using(SqlConnection conn = new SqlConnection(connStr))
        
{
            SqlCommand comm 
= new SqlCommand();
            comm.CommandText 
= "Select Top 1 * From T_LoginUser Where F_LoginName=@LoginName And F_PassWord=@Password";
            comm.Parameters.AddWithValue(
"@LoginName", username);
            comm.Parameters.AddWithValue(
"@Password", password);
            comm.Connection 
= conn;
            conn.Open();
            
using (SqlDataReader dr = comm.ExecuteReader())
            
{
                
if (dr.HasRows)
                
{                   
                   
return true;                    
                }

                
return false;
            }

        }
        
    }


    
/**//// <summary>
    
/// 创建用户
    
/// </summary>
    
/// <param name="username"></param>
    
/// <param name="password"></param>
    
/// <param name="email"></param>
    
/// <param name="passwordQuestion"></param>
    
/// <param name="passwordAnswer"></param>
    
/// <param name="isApproved"></param>
    
/// <param name="providerUserKey"></param>
    
/// <param name="status"></param>
    
/// <returns></returns>

    public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    
{

        
using (SqlConnection conn = new SqlConnection(connStr))
        
{
            SqlCommand comm 
= new SqlCommand();
            comm.CommandText 
= "Insert Into T_LoginUser(F_LoginName,F_Password) values(@LoginName,@Password)";
            comm.Parameters.AddWithValue(
"LoginName", username);
            comm.Parameters.AddWithValue(
"Password", password);
            comm.Connection 
= conn;
            conn.Open();
            
try
            
{
                comm.ExecuteNonQuery();
            }

            
catch (Exception Ex)
            
{
                
throw new Exception("创建用户失败!原因:" + Ex.Message.ToString());
            }


            MembershipUser user 
= new MembershipUser("MyMemberShipProvider", username, providerUserKey, email, passwordQuestion, "", isApproved, true, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
            status 
= MembershipCreateStatus.Success;
            
return user;
        }

    }


    
public override string ApplicationName
    
{
        
get
        
{
            
throw new Exception("暂未实现");
        }

        
set
        
{
            
throw new Exception("暂未实现");
        }

    }


    
public override bool ChangePassword(string username, string oldPassword, string newPassword)
    
{
        
throw new Exception("暂未实现");
    }


    
public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
    
{
        
throw new Exception("暂未实现");
    }


   

    
public override bool DeleteUser(string username, bool deleteAllRelatedData)
    
{
        
throw new Exception("暂未实现");
    }


    
public override bool EnablePasswordReset
    
{
        
get throw new Exception("暂未实现"); }
    }


    
public override bool EnablePasswordRetrieval
    
{
        
get throw new Exception("暂未实现"); }
    }


    
public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
    
{
        
throw new Exception("暂未实现");
    }


    
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
    
{
        
throw new Exception("暂未实现");
    }


    
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
    
{
        
throw new Exception("暂未实现");
    }


    
public override int GetNumberOfUsersOnline()
    
{
        
throw new Exception("暂未实现");
    }


    
public override string GetPassword(string username, string answer)
    
{
        
throw new Exception("暂未实现");
    }


    
public override MembershipUser GetUser(string username, bool userIsOnline)
    
{
        
throw new Exception("暂未实现");
    }


    
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
    
{
        
throw new Exception("暂未实现");
    }


    
public override string GetUserNameByEmail(string email)
    
{
        
throw new Exception("暂未实现");
    }


    
public override int MaxInvalidPasswordAttempts
    
{
        
get throw new Exception("暂未实现"); }
    }


    
public override int MinRequiredNonAlphanumericCharacters
    
{
        
get throw new Exception("暂未实现"); }
    }



    
public override int PasswordAttemptWindow
    
{
        
get throw new Exception("暂未实现"); }
    }


    
public override MembershipPasswordFormat PasswordFormat
    
{
        
get throw new Exception("暂未实现"); }
    }


    
public override string PasswordStrengthRegularExpression
    
{
        
get throw new Exception("暂未实现"); }
    }


    
public override bool RequiresUniqueEmail
    
{
        
get throw new Exception("暂未实现"); }
    }


    
public override string ResetPassword(string username, string answer)
    
{
        
throw new Exception("暂未实现");
    }


    
public override bool UnlockUser(string userName)
    
{
        
throw new Exception("暂未实现");
    }


    
public override void UpdateUser(MembershipUser user)
    
{
        
throw new Exception("暂未实现");
    }

}



顺便也给个MyRoleProvider.cs,不过啥也没干,放了一个空架子,呵呵
using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

/**/ /// <summary>
/// MyRoleProvider 的摘要说明
/// </summary>

public   class  MyRoleProvider:System.Web.Security.RoleProvider
{
    
public MyRoleProvider()
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }


    
// 摘要:
    
//     获取或设置要存储和检索其角色信息的应用程序的名称。
    
//
    
// 返回结果:
    
//     要存储和检索其角色信息的应用程序的名称。
    public override string ApplicationName 
    
{
        
get throw new Exception("暂未实现"); }
        
set throw new Exception("暂未实现"); }
    }


    
// 摘要:
    
//     将指定用户名添加到已配置的 applicationName 的指定角色名。
    
//
    
// 参数:
    
//   roleNames:
    
//     一个字符串数组,其中包含要将指定用户名添加到的角色的名称。
    
//
    
//   usernames:
    
//     一个字符串数组,其中包含要添加到指定角色的用户名。
    public override void AddUsersToRoles(string[] usernames, string[] roleNames) 
    
{
        
throw new Exception("暂未实现");
    }


    
//
    
// 摘要:
    
//     在数据源中为已配置的 applicationName 添加一个新角色。
    
//
    
// 参数:
    
//   roleName:
    
//     要创建的角色的名称。
    public override void CreateRole(string roleName) 
    
{
        
throw new Exception("暂未实现");
    }


    
//
    
// 摘要:
    
//     从数据源中移除已配置的 applicationName 的角色。
    
//
    
// 参数:
    
//   throwOnPopulatedRole:
    
//     如果为 true,则在 roleName 具有一个或多个成员时引发异常,并且不删除 roleName。
    
//
    
//   roleName:
    
//     要删除的角色的名称。
    
//
    
// 返回结果:
    
//     如果成功删除角色,则为 true;否则为 false。
    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) 
    
{
        
throw new Exception("暂未实现");
    }

    
//
    
// 摘要:
    
//     获取属于某个角色且与指定的用户名相匹配的用户名的数组。
    
//
    
// 参数:
    
//   usernameToMatch:
    
//     要搜索的用户名。
    
//
    
//   roleName:
    
//     作为搜索范围的角色。
    
//
    
// 返回结果:
    
//     一个字符串数组,包含用户名与 usernameToMatch 匹配且用户是指定角色的成员的所有用户的名称。
    public override string[] FindUsersInRole(string roleName, string usernameToMatch) 
    
{
        
throw new Exception("暂未实现");
    }

    
//
    
// 摘要:
    
//     获取已配置的 applicationName 的所有角色的列表。
    
//
    
// 返回结果:
    
//     一个字符串数组,包含在数据源中存储的已配置的 applicationName 的所有角色的名称。
    public override string[] GetAllRoles() 
    
{
        
throw new Exception("暂未实现");
    }

    
//
    
// 摘要:
    
//     获取指定用户对于已配置的 applicationName 所属于的角色的列表。
    
//
    
// 参数:
    
//   username:
    
//     要为其返回角色列表的用户。
    
//
    
// 返回结果:
    
//     一个字符串数组,其中包含指定用户对于已配置的 applicationName 所属于的所有角色的名称。
    public override string[] GetRolesForUser(string username) 
    
{
        
throw new Exception("暂未实现");
    }

    
//
    
// 摘要:
    
//     获取属于已配置的 applicationName 的指定角色的用户的列表。
    
//
    
// 参数:
    
//   roleName:
    
//     一个角色名称,将获取该角色的用户列表。
    
//
    
// 返回结果:
    
//     一个字符串数组,其中包含属于已配置的 applicationName 的指定角色的成员的所有用户名。
    public override string[] GetUsersInRole(string roleName) 
    
{
        
throw new Exception("暂未实现");
    }

    
//
    
// 摘要:
    
//     获取一个值,指示指定用户是否属于已配置的 applicationName 的指定角色。
    
//
    
// 参数:
    
//   username:
    
//     要搜索的用户名。
    
//
    
//   roleName:
    
//     作为搜索范围的角色。
    
//
    
// 返回结果:
    
//     如果指定用户属于已配置的 applicationName 的指定角色,则为 true;否则为 false。
    public override bool IsUserInRole(string username, string roleName) 
    
{
        
throw new Exception("暂未实现");
    }


    
//
    
// 摘要:
    
//     移除已配置的 applicationName 的指定角色中的指定用户名。
    
//
    
// 参数:
    
//   roleNames:
    
//     一个字符串数组,其中包含要将指定用户名从中移除的角色的名称。
    
//
    
//   usernames:
    
//     一个字符串数组,其中包含要从指定角色中移除的用户名。
    public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) 
    
{
        
throw new Exception("暂未实现");
    }


    
//
    
// 摘要:
    
//     获取一个值,该值指示指定角色名是否已存在于已配置的 applicationName 的角色数据源中。
    
//
    
// 参数:
    
//   roleName:
    
//     要在数据源中搜索的角色的名称。
    
//
    
// 返回结果:
    
//     如果角色名已存在于已配置的 applicationName 的数据源中,则为 true;否则为 false。
    public override bool RoleExists(string roleName) 
    
{
        
throw new Exception("暂未实现");
    }

}


下面是关键的Web。config配置
<? xml version="1.0" ?>

< configuration >
    
< appSettings />
    
< connectionStrings >
        
< add  name ="connStr"  connectionString ="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\demo.mdf;Integrated Security=True;User Instance=True"  providerName ="System.Data.SqlClient" />
    
</ connectionStrings >
    
< system.web >
        
        
< compilation  debug ="true" />
        
    
< authentication  mode ="Forms" >
      
< forms  defaultUrl ="~/Default.aspx"  name ="MemberShipProviderDemo"  cookieless ="UseCookies" ></ forms >
    
</ authentication >
    
    
    
        
< membership  defaultProvider ="MyMemberShipProvider" >
            
< providers >
                
< add  type ="MyMemberShipProvider"  name ="MyMemberShipProvider"  requiresQuestionAndAnswer ="false"  connectionString ="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\demo.mdf;Integrated Security=True;User Instance=True" />
            
</ providers >
        
</ membership >    
    
    
    
</ system.web >
</ configuration >

好了,随便建一个Default.aspx,放一个Login控件和 CreateUserWizard控件就可以测试了
目录
相关文章
|
5月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1008 152
|
5月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
808 156
|
5月前
|
SQL 人工智能 Linux
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
527 5
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
|
5月前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
5月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
434 158
|
5月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
5月前
|
缓存 监控 关系型数据库
使用MYSQL Report分析数据库性能(中)
使用MYSQL Report分析数据库性能
403 156
|
5月前
|
缓存 监控 关系型数据库
使用MYSQL Report分析数据库性能(上)
最终建议:当前系统是完美的读密集型负载模型,优化重点应放在减少行读取量和提高数据定位效率。通过索引优化、分区策略和内存缓存,预期可降低30%的CPU负载,同时保持100%的缓冲池命中率。建议每百万次查询后刷新统计信息以持续优化
517 161
|
6月前
|
存储 运维 关系型数据库
从MySQL到云数据库,数据库迁移真的有必要吗?
本文探讨了企业在业务增长背景下,是否应从 MySQL 迁移至云数据库的决策问题。分析了 MySQL 的优势与瓶颈,对比了云数据库在存储计算分离、自动化运维、多负载支持等方面的优势,并提出判断迁移必要性的五个关键问题及实施路径,帮助企业理性决策并落地迁移方案。
|
5月前
|
关系型数据库 MySQL 分布式数据库
阿里云PolarDB云原生数据库收费价格:MySQL和PostgreSQL详细介绍
阿里云PolarDB兼容MySQL、PostgreSQL及Oracle语法,支持集中式与分布式架构。标准版2核4G年费1116元起,企业版最高性能达4核16G,支持HTAP与多级高可用,广泛应用于金融、政务、互联网等领域,TCO成本降低50%。