From: http://hi.baidu.com/blfehhrehcbbsxq/item/d325b5cd12eb2c7688ad9e2f #
Membership, membershipuser, and roles
Membership, membershipuser, and roles
User and role management is implemented in ASP. net2.0 through the membership and roles classes.
Membership: User member account management, user name, password, email, etc.
Roles: manages the relationship between users and groups.
L membership: Mainly creates user accounts, verifies user identities, and manages user settings.
Attribute:
1) enablepasswordreset: gets a value to indicate whether the password can be reset.
2) enablepasswordretrieval: gets a value to indicate whether the user is allowed to extract the password.
3) hashalgorithmtype: an algorithm that obtains a value and a password.
4) maxinvalidpasswordattempts: Get a value and lock the number of retries allowed between users.
5) minrequirednonalphanumericcharacters: obtains the minimum number of special characters in the password.
6) minrequiredpasswordlength: Get the Minimum Password Length
7) requiresquestionandanswer: gets a value indicating whether to ask the user to answer the password question to re-extract the password.
8) userisonlinetimewindow: specify the number of minutes after the user's last activity date, during which the user is considered online.
Method:
1) createuser (): Creates a new user.
2) deleteuser (): deletes a user from the database
3) getuser (): obtains information of qualified users.
4) updateuser (): Updates user information
5) validateuser (): verify whether the user account and password are valid
6) getallusers (): obtains the set of users in the database.
7) findusersbyemail (): queries users by email
8) finndusersbyname (): queries a user by account name
9) getnumberofusersonline (): gets the online user of the current access program
10) getusernamebyemail (): queries the user name by email address
L membershipuser class
Manages and updates relevant materials in the membership table. Do not mix the membershipuser class with the membership class. It is actually handled by the membershipuser after it is processed. For example:
1) obtain or set user information. For example, username, email, and account creation time.
2) read and change the User Password
3) prompt questions and answers when changing passwords
4) Unlock an account
5) set whether to allow users to be verified
Attribute:
1) creationdate: obtains the User Creation Time.
2) Email: Get or set the user's email address
3) isapproved: gets or sets whether the user can perform verification.
4) islockedout: checks whether the user is locked.
5) isonline: indicates whether the user is online.
6) lastactivitydate: gets or sets the date and time of the last user verification or program access.
7) lastloclkoutdate: the date and time when the user was recently locked.
8) lastlogindate: gets or sets the date and time of the last user verification.
9) lastpasswordchangeddate: The last time the user password was updated.
10) passwordquestion: Obtain the user's security password.
11) Username: Get the User Login Name
Method:
12) changepassword (): Change the User Password
13) changepasswordquestionandanswer: change the password security questions and answers
14) GetPassword (): Get the User Password
15) resetpassword (): resets the user's password and automatically generates a new password.
16) unlockuser (): Unlock the account for Identity Authentication
L roles class
Method:
1) adduserstorole (): adds multiple users to a role.
2) adduserstoroles (): adds multiple users to multiple roles.
3) addusertorole (): adds a user to a role.
4) addusertoroles (): adds a user to multiple roles.
5) createrole (): Creates a role.
6) deleterole (): deletes a role.
7) findusersinrole (): searches for all users in a role.
8) getallroles (): obtains the list of all roles.
9) getrolesforuser (): obtains the list of roles of a user.
10) isuserinrole (): indicates whether the user is in the specified role.
11) removeuserfromrole (): removes a user from a role.
12) removeuserfromroles (): removes a user from multiple roles.
13) removeusersfromrole (): remove multiple users from a role
14) removeusersfromroles (): remove multiple user names from multiple roles
15) roleexists (): whether the user role name already exists in the role table.
Code implementation (1)
Create a user:
Membershipcreatestatus MC;
Membership. createuser (txtuid. Text, txtpwd. Text, txtemail. Text, txtquestion. Text, txtanswer. Text, true, out MC );
Response. Write (MC. tostring ());
Delete A User:
If (membership. deleteuser (txtuid. Text ))
Response. Write ("OK ");
Else
Response. Write ("error ");
Modify user information:
If (user. Identity. isauthenticated)
{
Membershipuser user = membership. getuser ();
User. Email = txtemail. text;
Membership. updateuser (User );
Response. Write (user. Email. tostring ());
}
Verify User Logon Information
Bool isok = membership. validateuser (txtuid. Text, txtpwd. Text );
If (isok)
{
Formsauthentication. setauthcookie (txtuid. Text, false );
Response. Write ("OK ");
}
Else
{
Response. Write ("error ");
}
Obtain login user information
If (user. Identity. isauthenticated)
{
Membershipuser myuser = membership. getuser ();
If (myuser! = NULL)
{
Response. Write (myuser. creationdate );
Response. Write (myuser. Email );
Response. Write (myuser. islockedout );
Response. Write (myuser. isonline );
Response. Write (myuser. passwordquestion );
Response. Write (myuser. username );
}
}
Return the User Password Based on the answer to the password question
If (user. Identity. isauthenticated)
{
Membershipuser user = membership. getuser (txtuid. Text );
Txtpwd. Text = user. GetPassword (txtanswer. Text );
}
Change User Password
If (user. Identity. isauthenticated)
{
Membershipuser user = membership. getuser ();
User. changepassword (txtpwd. Text, txtpwd2.text );
Response. Write (user. GetPassword (txtanswer. Text ));
}
Unlock a user
Membershipuser user = membership. getuser (txtuid. Text );
Bool B = user. unlockuser ();
Response. Write (B + "" + User. islockedout );
User Logon denied
Membershipuser user = membership. getuser (txtuid. Text );
User. isapproved = false;
Membership. updateuser (User );
Response. Write (user. islockedout );
Allow User Login
Membershipuser user = membership. getuser (txtuid. Text );
User. isapproved = true;
Membership. updateuser (User );
Response. Write (user. islockedout );
Retrieve users by user name or email address
Membershipusercollection users;
Switch (listtype. Text)
{
Case "name ":
Users = membership. findusersbyname (txtfind. Text );
If (users. Count> 0)
{
Showuserinfo (users );
}
Else
{
Response. Write ("User Name Not Found ");
}
Break;
Case "email ":
Users = membership. findusersbyemail (txtfind. Text );
If (users. Count> 0)
{
Showuserinfo (users );
}
Else
{
Response. Write ("No email found ");
}
Break;
}
Code Implementation (2)
Load all users
Membershipusercollection user = membership. getallusers ();
Listuser. datasource = user;
Listuser. databind ();
Load all roles
String [] role = roles. getallroles ();
Listrole. datasource = role;
Listrole. databind ();
Add a new role
Roles. createrole (txtrole. Text );
Delete a role
Roles. deleterole (txtrole. Text );
Add a user to a role
Roles. addusertorole (listuser. Text, listrole. Text );
Remove a user from a role
Roles. removeuserfromrole (listuser. Text, listrole. Text );
Add a user to multiple roles
Int n = 0;
Foreach (listitem Li in listrole. Items)
{
If (Li. Selected) n ++;
}
String [] roles = new string [N];
Int I = 0;
Foreach (listitem Li in listrole. Items)
{
If (Li. Selected)
{
Roles [I ++] = Li. text;
}
}
Roles. addusertoroles (listuser. Text, roles );
Remove all roles of a user
String [] STR = roles. getrolesforuser (listuser. Text );
Roles. removeuserfromroles (listuser. Text, STR );
Add multiple users to a role
Int n = 0;
Foreach (listitem Li in listuser. Items)
{
If (Li. Selected) n ++;
}
String [] users = new string [N];
Int I = 0;
Foreach (listitem Li in listuser. Items)
{
If (Li. Selected)
{
Users [I ++] = Li. text;
}
}
Roles. adduserstorole (users, listrole. Text );
Remove all users in a role
String [] users = roles. getusersinrole (listrole. Text );
Roles. removeusersfromrole (users, listrole. Text );
Add multiple users to multiple roles
Int n = 0;
Foreach (listitem Li in listuser. Items)
{
If (Li. Selected) n ++;
}
String [] struser = new string [N];
N = 0;
Foreach (listitem Li in listrole. Items)
{
If (Li. Selected) n ++;
}
String [] strroles = new string [N];
For (INT I = 0; I <listuser. Items. Count; I ++)
{
Struser [I] = listuser. items [I]. text;
}
For (INT I = 0; I <listrole. Items. Count; I ++)
{
Strroles [I] = listrole. items [I]. text;
}
Roles. adduserstoroles (struser, strroles );