Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is an example to get the job done. bool isValid is set by looking at some data store (lets say your user data base). UserID is just an ID i am maintaining. You can add aditional information like email address to user data.</p> <pre><code>protected void btnLogin_Click(object sender, EventArgs e) { //Hard Coded for the moment bool isValid=true; if (isValid) { string userData = String.Empty; userData = userData + "UserID=" + userID; FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(30), true, userData); string encTicket = FormsAuthentication.Encrypt(ticket); HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); Response.Cookies.Add(faCookie); //And send the user where they were heading string redirectUrl = FormsAuthentication.GetRedirectUrl(username, false); Response.Redirect(redirectUrl); } } </code></pre> <p>in the golbal asax add the following code to retrive your information</p> <pre><code>protected void Application_AuthenticateRequest(Object sender, EventArgs e) { HttpCookie authCookie = Request.Cookies[ FormsAuthentication.FormsCookieName]; if(authCookie != null) { //Extract the forms authentication cookie FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); // Create an Identity object //CustomIdentity implements System.Web.Security.IIdentity CustomIdentity id = GetUserIdentity(authTicket.Name); //CustomPrincipal implements System.Web.Security.IPrincipal CustomPrincipal newUser = new CustomPrincipal(); Context.User = newUser; } } </code></pre> <p>When you are going to use the information later, you can access your custom principal as follows. </p> <pre><code>(CustomPrincipal)this.User or (CustomPrincipal)this.Context.User </code></pre> <p>this will allow you to access custom user information.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload