From: http://www.lemongtree.com/2006/11/28/0000572.aspx
A friend on csdn asked this question and wrote it later.
Why do we need to customize it?
First, customization is more flexible and does not rely too much on the aspnetdb database, which facilitates your own expansion. Second, you can still use the login control in. NET 2.0 after customization.
Of course, custom is not customized at will, but two abstract classes are implemented:
Membershipprovider and roleprovider
For more information about these two abstract classes, see SDK 2.0 or msdn.
In this demo, the methods to be rewritten in membershipprovider include:Validateuser
Because I only want to verify the user, to create and edit, we need to implement the updateuser method and createuser method.
Check the override of the validateuser method: [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 ;
}
}
I have written the connectionstring variable in. cs. This is just for demonstration. It should be written in Web. config. I believe you can understand this method.
This achieves user verification.
The following describes how to verify the role [myrole. CS]
The methods to be implemented include:
Bool isuserinrole (string username, string rolename)
Used to verify whether a user belongs to a specified role
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 ;
}
}
}
CodeSimple.
The second method to be implemented:
String [] getrolesforuser (string username) to obtain the list of all roles of the current user
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 (',');
}
}
}
ReturnTMP;
}
The following figure shows how to change web. config.
First, perform forms verification on the relevant pages.
<Authentication mode = "forms">
<Forms defaulturl = "default. aspx" loginurl = "userlogin. aspx" Path = "/" name = "Demo"/>
</Authentication>
Pay attention to the following configuration, with the highest priority
< 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 \ Desktop \ demo. mdb; persist Security info = false" / >
< /Providers >
< /Membership >
< Rolemanager defaultprovider = "Myrole" Enabled = "True" >
< Providers >
< Add name = "Myrole" Type = "Myrole" / >
< /Providers >
< /Rolemanager >
Note that enabled in rolemanager must be true, otherwise it will become invalid.
< 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 >
Perform different role control on admin. aspx and guest. aspx.
Tired. Check the demo directly.
Username and password
Admin
Guest guest
Download:
Http://files.cnblogs.com/kagar/framework.rar