Post-"ASP. NET forms-based authentication mechanism, record it

Source: Internet
Author: User

the forms-based authentication mechanism is constructed as follows:
1. Set IIS to anonymous access and Asp.net web. set "Form Verification" in config
2. retrieve the data storage to authenticate the user and retrieve the role (if not based on the role)
3, use formsauthenticationticket to create a cookie and send it back to the client, and store the
role to the ticket, for example:
formsauthentication. setauthcookie (username, true | false)
cookie retention time:
httpcontext. current. response. cookies [formsauthentication. formscookiename]. expires = datetime. now. adddays (1)

to store a role, use:
formsauthenticationticket authticket = new
formsauthenticationticket (
1, // version
txtusername. text, // user name
datetime. now, // creation
datetime. now. addminutes (20), // expiration
false, // persistent
roles ); // user data
roles is a role String Array
string encryptedticket = formsauthentication. encrypt (authticket); // encryption

Store Cookie
Httpcookie authcookie =
New httpcookie (formsauthentication. formscookiename,
Encryptedticket );

Response. Cookies. Add (authcookie );

4. Process in application_authenticaterequest eventProgramIn (Global. asax), use
The iprincipal object is created for the ticket and exists in httpcontext. User.
Code:
Httpcookie authcookie = context. Request. Cookies [formsauthentication. formscookiename];
Formsauthenticationticket authticket = formsauthentication. decrypt (authcookie. Value); // decrypt
String [] roles = authticket. userdata. Split (New char [] {';'}); // It is decomposed according to the stored format; or | ....
Context. User = new genericprincipal (context. User. Identity, roles); // In httpcontext. User

5. There are two ways to control roles on some pages:
5.1, add in Web. config
<Location Path = "editpost. aspx">
<System. Web>
<Authorization>
<Allow roles = "rolename"/>
<Deny users = "? "/>
</Authorization>
</System. Web>
</Location>
5.2 put files accessible only by some roles in the same directory, and add a web. config
<Configuration>
<System. Web>
<Authorization>
<Allow roles = "rolename"/>
<Deny users = "*"/>
</Authorization>
</System. Web>
</Configuration>
Note: The Web. config setting of sub-directories takes precedence over the web. config setting of the parent directory.

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.