ASP. net2.0 role management, detailed steps

Source: Internet
Author: User
Step 1:
C # code
   <authentication mode="forms"><forms name=".ASPXAUTH " loginUrl="/login.aspx" timeout="30" path= "/"></forms></authentication>

Step 2:
Create web. config in the directory under the role control, as shown in the following Configuration:

C # code
   <authorization><allow users="comma-separated list of users"roles="comma-separated list of roles"verbs="comma-separated list of verbs" /><deny users="comma-separated list of users"roles="comma-separated list of roles"verbs="comma-separated list of verbs" /></authorization>

Step 3:
Login code to get a ticket

C # code
   FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket (1,user,DateTime.Now,
Datetime. now. addminutes (30), false, userroles, "/"); // create an identity authentication ticket object string hashticket = formsauthentication. encrypt (ticket); // The encrypted serialization validation ticket is a string httpcookie usercookie = new httpcookie (formsauthentication. formscookiename, hashticket); // generate cookie context. response. cookies. add (usercookie); // cookie

Step 4: (manually create a role)
In global. asax

C # code
   Protected void application_authorizerequest (Object sender, system. eventargs e) {httpapplication APP = (httpapplication) sender; httpcontext CTX = app. context; // obtain the httpcontext object related to this HTTP request if (CTX. request. isauthenticated = true) // The authenticated user performs role processing {formsidentity id = (formsidentity) CTX. user. identity; formsauthenticationticket ticket = ID. ticket; // get the authentication ticket string [] roles = ticket. userdata. split (','); // convert the role data in the authentication ticket to a string array CTX. user = new genericprincipal (ID, roles); // Add the original identity with the role information to create a genericprincipal to indicate the current user, so that the current user has the role information }}
Related Article

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: 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.