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 }}