Create a standard routine using the. NET 2.0 membership function (2)-Configuration

Source: Internet
Author: User
From: http://www.cnblogs.com/waynebaby/archive/2006/12/13/590699.html

Since. NET 2.0, Asp.net has entered an advocate for less writing.CodeMulti-configuration Period

"Machine is advanced. We only need to drive the pig across the road and the sausage will be sent from the conveyor belt !"

The following describes how to start a machine and catch pigs.

Before catching up with pig, let's take a look at how Web. Security works.

When a page is opened, the system first reads system. Web and creates corresponding providers in the process according to the configuration section.
All subsequent login controls automatically call the methods and properties of the corresponding provider instance during interaction. Therefore, most of the work is performed in configuration.

Many of the objects in the component are recommended to write by myself, so as not to rely too much on aspnetdb. For details, refer to the fantasy article.
However, this is a standard routine, so it is currently based on the standard library of M $.

The specific configuration is as follows:

1. Configure the database
Run aspnet_regsql.exe under \ windows \ Microsoft. NET \ framework \ v2.0.50727.

Then the wizard creates the corresponding table in SQL Server.
This
Some tables are the tables corresponding to the. NET 2.0 default provider. If you create a new project
There is no need to write annoying login on your own. These tables and their supported default providers already cover many advanced functions, including cookie support, cross-site and cross-process login, and password retrieval
Code, email authentication and other functions, of course, we can also expand them.

We also need to establish this data connection string in Web. config (in the <configuration> segment)

< Connectionstrings >
< Add Name = "Membership_connection" Connectionstring = "Data Source =.; initial catalog = membership_demo; persist Security info = true; user id = sa; Password = abcdefg" Providername = "System. Data. sqlclient" />
</ Connectionstrings >

In this way, the database configuration has been completed. What does my kitchen say? Yin dry to be used.

2. Configure the verification type

An Asp.net default authentication is Windows NT login. If we want to process the verification information by ourselves, we need to add the node Notification System in the <system. Web> section below <configuration>.

< Authentication Mode = "Forms" >
< Forms Loginurl = "~ \ Membership \ authing \ login. aspx" Timeout = "30000" >
</ Forms >
</ Authentication >

Mode = forms indicates that I do not use the default Windows NT permission settings, but use the custom web page-oriented authentication method we configured.
Loginurl indicates which address to jump to (usually with a login Control) if you need to verify the access to a page. After login, the system will jump back to the previous page.

3. Configure various providers
The provider feels a bit like the dataadapter feels like dataset and is a selectable accessory. If you configure different providers, different user databases can be accessed.
Currently, provider provides two official providers by default: SQL provider & ADSI provider
The former is saved through the sqlserver database, and the latter is directly linked to the ADSI service in the Windows domain.

For example < Membership Defaultprovider = "Main_sqlmembershipprovider" Userisonlinetimewindow = "20" >
< Providers >
< Add Connectionstringname = "Membership_connection" Enablepasswordretrieval = "False" Enablepasswordreset = "True" Requiresquestionandanswer = "True" Passwordformat = "Hashed" Applicationname = "Demo_membership" Name = "Main_sqlmembershipprovider" Type = "System. Web. Security. sqlmembershipprovider" />

</ Providers >
</ Membership >

In this example, we will use the membership_connection connection string to connect to the database (we will use the negative part)
You cannot retrieve the password, but you can reset the password. When you retrieve the password, you need to ask questions about the password, and use sha1 to encrypt the password stored in the database. The name of a common membership database is demo_membership.
Type
Is an important option, which determines which type of interface you use to serve as a provider. If you use your own provider
You can add the DLL as a reference to the bin directory and enter the detailed namespace information here, such as "" system. Windows. forms,
Version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089"
I will not go into details here.

At this time, we can apply for and log on through the control. All you need to do is drag the control to the specified page, and test can also be modified through the management tool under the website menu.

Some options may not be available, but you can try to delete and modify them. We only need to further configure more features to open the door to us ~~~

Similarly, we configure the provider of roles and profile. < Rolemanager Enabled = "True" Cacherolesincookie = "True" Cookiename = ". Asproles" Cookierequiressl = "True" Defaultprovider = "Sqlroleprovider" >
< Providers >
< Add Connectionstringname = "Membership_connection" Applicationname = "Demo_membership" Name = "Sqlroleprovider" Type = "System. Web. Security. sqlroleprovider" />
</ Providers >
</ Rolemanager > < Profile Enabled = "True" Defaultprovider = "Sqlprofileprovider" >
< Providers >
< Add Name = "Sqlprofileprovider" Type = "System. Web. profile. sqlprofileprovider" Connectionstringname = "Membership_connection" Applicationname = "Demo_membership" Description = "Sqlprofileprovider for sampleapplication" />
</ Providers >
< Properties >

< Add Name = "Userage" Type = "System. String" Defaultvalue = "21" > </ Add >
< Add Name = "Usersex" Type = "System. String" Defaultvalue = "Male" > </ Add >
</ Properties >
</ Profile >

After compilation, you can easily access and set the information of the users you are currently logged on to on your page:

Protected   Sub button#click () Sub Button#click ( Byval Sender As   Object , Byval E As System. eventargs) Handles Button1.click
Dim X As   String   =   String . Empty
X = X & Membership. getuser (). Username &   " : "
X = X & Roles. getrolesforuser (membership. getuser (). username). Length &   " Roles: "
X = X &   " Age "   & Profile. userage
Button1.text = X

End sub

If you do not log on, you may get an object reference error. However, if you log on as a known user, the running result is probably like this.

The website management tool just mentioned can basically work normally.

The configuration example is completed here. As for the meaning of each parameter, msdn is still quite complete. If it is inconvenient to query the information, leave a message for discussion.

In the next chapter, we will bind the data to the gridview and formview for management.

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.