Membership configuration)

Source: Internet
Author: User
Tags connectionstrings
Since. NET 2.0, Asp.net has entered a period of advocating less code and more configuration.

"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.
These tables are. NET 2.0 default provider corresponding table, if you create a new project, there is no need to write annoying login in your own, these tables and their supported default providers already cover many advanced features, including cookie support, cross-site and cross-process login, password retrieval, 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" Success = "false" enablepasswordreset = "true" Success = "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 the interface you use to serve as the provider. If you use your own provider, you can add the DLL as a reference to the bin directory and enter it here. detailed namespace information, such as "system. windows. forms, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089 "and other things are not described 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.

 

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/tigercopy/archive/2007/10/11/1819733.aspx

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.