Introduction to ASP. NET authentication mechanism membership-API

Source: Internet
Author: User

I don't know why. Recently, I have always been dizzy, and I don't feel like I am doing anything. I feel so tired when I am busy with my work ~ However, in any case, you must stick to it when writing a blog, at least January 1 articles ~ This is a task for you.

Back to the topic, I mentioned how to configure membership and how to use the login control provided by Asp.net. This shows how to compileCodeTo complete the corresponding functions.

To use membership, we need to introduceSystem. Web. SecurityThis namespace. There are several frequently used classes in this namespace:

    1. Membership
    2. Roles

 

First, we will introduce the first membership class. The membership class is a static class that provides all user-related operations, such as registering a user, deleting a user, and obtaining a user list, searching for users by email, etc.

There are many methods, but most of them are very simple. Let's pick two more troublesome ones to explain in detail:

The membership. createuser method is used to create a user by name. This method has four reloads:

 

Public   Static Membershipuser createuser ( String Username, String Password );

Public StaticMembershipuser createuser (StringUsername,StringPassword,StringEmail );

Public StaticMembershipuser createuser (StringUsername,StringPassword,StringEmail,StringPasswordquestion,StringPasswordanswer,BoolIsapproved,OutMembershipcreatestatus status );

Public   Static Membershipuser createuser ( String Username, String Password, String Email, String Passwordquestion, String Passwordanswer, Bool Isapproved, Object Provideruserkey, Out Membershipcreatestatus status );

 

The returned values of these four reloads areMembershipuserClass,MembershipuserA class is actually an entity class, but it includes some methods, such as getting a password and changing a password. Which overload should be used? Do you still remember the previous web. config configuration? For example, if requiresquestionandanswer is set to true, you must use the third or fourth overload to provide the question and answer.IsapprovedThis parameter indicates whether the created user is activated. LastMembershipcreatestatusIs an enumeration, which is modified with the out keyword to obtain the status of the created user, for example:Membershipcreatestatus. SuccessThe user is successfully created,Membershipcreatestatus. invalidemailThe format of the email is incorrect.ProvideruserkeyIs a guid type data used to specify the user's userid.

 

Another thing to introduce is public static bool validateuser (string username, string password). In fact, we should guess the role of this method by name and parameter, the two parameters are the user name and password respectively, and the return value is whether the logon is successful. The remaining methods are very simple. The validateuser method is often used in combination with the formsauthentication class. The formsauthentication class provides a series of static methods to manage forms authentication services.

In the formsauthentication class, common methods include:

Public   Static   String Hashpasswordforstoringinconfigfile ( String Password, String Passwordformat ); Public Static VoidSetauthcookie (StringUsername,BoolCreatepersistentcookie );
Public   Static   Void Redirectfromloginpage ( String Username, Bool Createpersistentcookie );

 

The first hashpasswordforstoringinconfigfile method has a simple function. It hashes the input password according to the hash specified by passwordformat.AlgorithmGenerate the hash password and return it. The passwordformat value can be "MD5" or "sha1.

The second setauthcookie method sends an authentication ticket to the client. Username is the Login User Name. createpersistentcookie is used to tell Asp.net whether to create a persistent cookie, so that the user can access it again.

The third redirectformloginpage method redirects authenticated users back to the original requested URL or default URL.

This is usually the usage:

 

If (Membership. validateuser ( " Zhangsan " , " 123123 " ))
{
Formsauthentication. redirectfromloginpage ( " Zhangsan " , False );
}

 

 

The roles class is also a static class, which encapsulates all role-related methods. For example, common operations such as creating a role, adding a user to a role, and deleting a role. To use the roles class, note that the rolemanager section in the web. config configuration must be correctly configured and the enabled attribute must be set to true.

In the roles class, the addusertorole method is required. This method is usually used with membership. createuser, for example:

If (Membership. createuser ( " Zhangsan " , " 123123 " ))
{
// The user is successfully created, and the user role is assigned to zhangsan.
Roles. addusertorole ( " Zhangsan " , " User " );
}

 

In this way, after the user zhangsan is created, the user role is assigned to zhangsan. You can make the following configuration in the configuration file:

< Configuration >
< Appsettings />
< Connectionstrings />
< System. Web >
< Authorization >
< Allow Roles = "User" />
< Deny Users = "? " />
</ Authorization >
</ System. Web >
</ Configuration >

In this way, all newly registered members can access resources in the current directory. For specific configuration, see ASP. NET authentication mechanism membership introduction-configuration (2 ).

 

OK. The API section will be introduced here.

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.