当然,自定义不是随便自定义,而要是实现二个抽象类:
MembershipProvider和RoleProvider
这二个抽象类的说明请参见SDK 2.0或MSDN,这里不多说了。
在这个Demo中,MembershipProvider中要重写的方法有:ValidateUser
因为我只是为了验证用户,要实现创建、编辑,就要实现UpdateUser方法、CreateUser方法了。
看ValidateUser方法的重写:[MyMemberShip.cs]
public override bool ValidateUser(string username, string password)
{
using (OleDbConnection conn = new OleDbConnection(connectionstring))
{
OleDbCommand comm = new OleDbCommand();
comm.CommandText = "select count(0) from users where u_name=@name and u_pwd=@pwd";
comm.Parameters.AddWithValue("@name", username);
comm.Parameters.AddWithValue("@pwd", password);
comm.Connection = conn;
conn.Open();
return ((int)comm.ExecuteScalar()) > 0 ? true : false;
}
}
{
using (OleDbConnection conn = new OleDbConnection(connectionstring))
{
OleDbCommand comm = new OleDbCommand();
comm.CommandText = "select count(0) from users where u_name=@name and u_pwd=@pwd";
comm.Parameters.AddWithValue("@name", username);
comm.Parameters.AddWithValue("@pwd", password);
comm.Connection = conn;
conn.Open();
return ((int)comm.ExecuteScalar()) > 0 ? true : false;
}
}
这里的变量connectionstring我写在.cs里了,这只是为了演示,应该写在web.config中的。这个方法不用多说,相信大家能看明白。
这就实现了用户的验证。
下面实现角色的验证[MyRole.cs]
要实现的方法有:
bool IsUserInRole(string username, string roleName)
用于验证用户是否属于指定的角色
public override bool IsUserInRole(string username, string roleName)
{
using (OleDbConnection conn = new OleDbConnection(connectionstring))
{
OleDbCommand comm = new OleDbCommand();
comm.CommandText = "select top 1 * from users where u_name=@name and u_role=@role";
comm.Parameters.AddWithValue("@name", username);
comm.Parameters.AddWithValue("@role", roleName);
comm.Connection = conn;
conn.Open();
using (OleDbDataReader dr = comm.ExecuteReader())
{
if (dr.HasRows)
{
return true;
}
return false;
}
}
}
{
using (OleDbConnection conn = new OleDbConnection(connectionstring))
{
OleDbCommand comm = new OleDbCommand();
comm.CommandText = "select top 1 * from users where u_name=@name and u_role=@role";
comm.Parameters.AddWithValue("@name", username);
comm.Parameters.AddWithValue("@role", roleName);
comm.Connection = conn;
conn.Open();
using (OleDbDataReader dr = comm.ExecuteReader())
{
if (dr.HasRows)
{
return true;
}
return false;
}
}
}
代码简单,也不多说了
第二个要实现的方法:
string[] GetRolesForUser(string username),取得当前用户的所有角色列表
public override string[] GetRolesForUser(string username)
{
string[] tmp = new string[] { };
using (OleDbConnection conn = new OleDbConnection(connectionstring))
{
OleDbCommand comm = new OleDbCommand();
comm.CommandText = "select top 1 * from users where u_name=@name";
comm.Parameters.AddWithValue("@name", username);
comm.Connection = conn;
conn.Open();
using (OleDbDataReader dr = comm.ExecuteReader())
{
if (dr.Read())
{
tmp = dr["U_role"].ToString().Split(',');
}
}
}
return tmp;
}
{
string[] tmp = new string[] { };
using (OleDbConnection conn = new OleDbConnection(connectionstring))
{
OleDbCommand comm = new OleDbCommand();
comm.CommandText = "select top 1 * from users where u_name=@name";
comm.Parameters.AddWithValue("@name", username);
comm.Connection = conn;
conn.Open();
using (OleDbDataReader dr = comm.ExecuteReader())
{
if (dr.Read())
{
tmp = dr["U_role"].ToString().Split(',');
}
}
}
return tmp;
}
下面就是更改web.config了
首先,需要对相关页作forms验证
<authentication mode="Forms">
<forms defaultUrl="default.aspx" loginUrl="userlogin.aspx" path="/" name="Demo"/>
</authentication>
注意下面的配置,重中之重
<membership defaultProvider="MyMemberShip">
<providers>
<add name="MyMemberShip" type="MyMemberShip" requiresQuestionAndAnswer="true" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\Documents and Settings\Administrator\桌面\Demo\demo.mdb;Persist Security Info=False"/>
</providers>
</membership>
<roleManager defaultProvider="MyRole" enabled="true">
<providers>
<add name="MyRole" type="MyRole"/>
</providers>
</roleManager>
<providers>
<add name="MyMemberShip" type="MyMemberShip" requiresQuestionAndAnswer="true" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\Documents and Settings\Administrator\桌面\Demo\demo.mdb;Persist Security Info=False"/>
</providers>
</membership>
<roleManager defaultProvider="MyRole" enabled="true">
<providers>
<add name="MyRole" type="MyRole"/>
</providers>
</roleManager>
注意roleManager中的enabled一定要为true,否则会失效。
<location path="admin.aspx">
<system.web>
<authorization>
<allow roles="admin"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="guest.aspx">
<system.web>
<authorization>
<allow roles="guest"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
对admin.aspx与guest.aspx做不同的角色控制<system.web>
<authorization>
<allow roles="admin"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="guest.aspx">
<system.web>
<authorization>
<allow roles="guest"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
累了,直接看demo吧
用户名 密码
admin admin
guest guest
附件:/MemberShip.rar