Use of the membership and membershipuser classes (2)

Source: Internet
Author: User
From huohuo's blog
The membership API is made public through two new classes: Membership and membershipuser. The former includes static methods for creating users, verifying users, and completing other work. Membershipuser represents a single user. It contains methods and attributes used to retrieve and change passwords, obtain the last logon date, and complete similar work. With these two new classes, we can easily manage users without writing a line of code. However, in the actual development process, it cannot meet our daily development needs. After daily project development and online data search, we will list them one by one:

1. By default, all types of databases use SQL Express. In actual development, we often use SQL Sever 2000 or SQL Server 2005. In this case, we need to modify the database type.

Microsoft provides us with an aspnet_regsql command to modify the default database. Open the Visual Studio 2005 command prompt, enter aspnet_regsql, and follow the prompts to proceed step by step.

When you open the database, you can find a series of stored procedures starting with "ASPnet _" in multiple places. This is the stored procedure necessary for using membership.

Open IIS, [properties] → [ASP. NET] → [edit configuration]:

[General], the connection parameter localsqlserver follows the normal SQL connection string format.

[Authentication], the mode is forms, and the minrequirednonalphanumericcharacters of the management provider is 0. In this case, you can remove the default abnormal password that requires a combination of letters and numbers. In this step, you can also change the minimum length and maximum length of the password.

After this step, the system automatically configures the required rules in Web. config. You can use this graphical tool to modify web. config.

2. the built-in login control and membership class only provide simple user information input, which cannot meet the needs of our project. For example, you must enter a QQ number, phone number, and home address when registering a user. By default, there is no solution. Here are two solutions. I used them in different projects. You can determine the advantages and disadvantages.

1. Use profile. There are already many online tutorials on this method. I will not repeat the description to avoid the suspicion of making a draft fee :). Here is only a description that is not provided on the Internet.
Because membership can only list the user names of a specified group, but cannot list other details, we often need to modify other information in the group at the same time in actual use. I use a self-built datatable method. See the Code:

Public static datatable listuser (string userroles) // lists the user information of a specified group.
{
String [] users = roles. getusersinrole (userroles );
// List users in a specified group
Datatable dt = new datatable ();
DT. Columns. Add ("username", system. type. GetType ("system. String "));
DT. Columns. Add ("QQ", system. type. GetType ("system. String "));
DT. Columns. Add ("phone", system. type. GetType ("system. String "));
DT. Columns. Add ("Address", system. type. GetType ("system. String "));
DT. Columns. Add ("email", system. type. GetType ("system. String "));
// Construct a data table
Foreach (string I in Users)
{
Datarow DR = DT. newrow ();
Membershipuser mu = membership. getuser (I );
Obtain basic user information
Profilecommon P = profile. getprofile (I); // obtain the user's profile information
Dr [0] = mu. Username;
Dr [1] = P. QQ;
// The profile is strongly typed and can be easily added through perception.
Dr [2] = P. Phone;
Dr [3] = P. address;
Dr [4] = mu. Email;
DT. Rows. Add (DR );
DT. acceptchanges ();
}
Return DT;
}
Public static void deleteuser (string username)/delete a specified user
{
Membership. deleteuser (username );
// The system automatically deletes the specified user information under the profile.
}
Public static void updateuser (string username)/update a specified user
{
Profilecommon P = profile. getprofile (I );
// Obtain the user's profile information
P. Phone = "phone ";
P. Address = "address;
P. QQ = "QQ number ";
P. Save ();
// Save the modification.
}

2. Customize a membershipinfo table and associate it with the membership system logo. Write your own SQL statements for query, modification, and other functions.

List users in a specified group
Select * From aspnet_membership inner join aspnet_users on
Aspnet_membership.userid = aspnet_users.userid left join memberinfo on aspnet_membership.userid = memberinfo. userid
Where aspnet_membership.userid = (select userid from aspnet_usersinroles inner join
Aspnet_roles on aspnet_usersinroles.roleid = aspnet_roles.roleid where rolename = 'admin ')

  
Deletion, modification, and other functions are relatively simple and will not be described here. You can use the createuser method of membership and then use SQL statements to write data to the memberinfo table.

To create a user, we can extend the createuserwizard control or write the logon interface on our own.
1. Extend the createuserwizard control. You can use its template column. Note the following: User Name, password, question, answer, email, their IDs must be username, password, question, answer, and email. Otherwise, an error occurs and the verification control cannot be used at this time. Suspected to be a bug in IDE.

As shown below, the defined style should be:

<Wizardsteps>
<Asp: createuserwizardstep runat = "server"> Custom Code <contenttemplate>
</Contenttemplate>
</ASP: createuserwizardstep>
</Wizardsteps>
Code Section:
Protected void createuserwizardinclucreateduser (Object sender, eventargs E)
{
// Because the system will automatically create basic information tables for you, you only need to modify the profile or membershipinfo mark.
Roles. addusertorole (createuserwizard1.username, "Shop ");
// Add a user to the corresponding group
Profilecommon P = (profilecommon) profilecommon. Create (createuserwizard1.username, true );
P. QQ = (textbox) createuserwizard1.createuserstep. contenttemplatecontainer. findcontrol ("QQ"). Text. Trim ();
P. Address = (textbox) createuserwizard1.createuserstep. contenttemplatecontainer. findcontrol ("Address"). Text. Trim ();
P. Phone = (textbox) createuserwizard1.createuserstep. contenttemplatecontainer. findcontrol ("phone"). Text. Trim (); p. Save (); // Save the modification
}

2. Write the UI by yourself. This method is recommended for personal use. It is flexible and can use 2005 powerful verification controls.

The page section is relatively simple and skipped. The Code section is described in detail below.

In this case, we need to use the createuser () method of membership. We will not introduce the syntax. It will return a membershipcreatestatus enumeration class based on the result of creating a user. It contains detailed error information about all Failed User Creation. You only need to return the corresponding prompt to the interface based on its value, such as the user name already exists and the email already exists.

We can still use the profile or custom membershipinfo table method for user information.

List the methods used to create a user for a profile. SQL statements are relatively simple and skipped.

Protected void button#click (Object sender, eventargs E)
{
Membershipcreatestatus status;
Membershipuser newuser = membership. createuser (username. text. trim (), password. text. trim (), email. text. trim (), question. text. trim (), answer. text. trim (), true, out status );
// Use membership to create a user and return the result to membershipcreatestatus

If (newuser = NULL) // if no new user exists, an error occurs.
{
Geterrormessage (Status );
// Call the geterrormessage function to return detailed error information
} Else {
Roles. addusertorole (newuser. username, "Jiancai ");
// Add the user to the corresponding group profilecommon
P = (profilecommon) profilecommon. Create (newuser. username, true );
P. QQ = QQ. Text. Trim ();
P. Address = address. Text. Trim ();
P. Phone = phone. Text. Trim ();
P. Save ();
}
}
Public void geterrormessage (membershipcreatestatus status)
{
Switch (Status)
{
Case membershipcreatestatus. duplicateusername: displayalert ("the current user already exists. Please reselect ");
Break;
// For other error messages, see the membershipcreatestatus enumeration class of msdn.
Default: displayalert ("failed to register 00000000 users. Please check your username, password, and other information ");
Break;
}
}

Personal Opinion: it is convenient to adopt the profile method. Because the profile is of a strong type, you can use the smart sensing function to reduce the input of code. When using a custom data table method, you need to input a large number of SQL statements, but the query speed is relatively fast and the performance is relatively strong, due to roles. the getusersinrole () method cannot read data by PAGE, and only all data can be read at a time. Self-writing SQL statements can be easily combined with root pages. The profile method is not recommended as the number of users increases.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.