Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Another answer, just to show how you might want to create your cookie using the values from the web.config instead of hardcoding them in code.</p> <p>First off, consider if you need all the extra options. The simplest is to have everything setup in your web.config</p> <pre><code>FormsAuthentication.RedirectFromLoginPage("Bob", isPersistent) </code></pre> <p>However, if you need to add UserData to the ticket, you will have to create your own. Note how we use the values in the web.config instead of hard coding values.</p> <pre><code>/// &lt;summary&gt; /// Create a New Forms Authentication Ticket when User Impersonation is active, using the current ticket as a basis for the new ticket. /// &lt;/summary&gt; private static void NewTicket(MyUser currentUser, string userData, bool createPersistentCookie) { System.Web.Configuration.AuthenticationSection authSection = (System.Web.Configuration.AuthenticationSection) ConfigurationManager.GetSection("system.web/authentication"); System.Web.Configuration.FormsAuthenticationConfiguration formsAuthenticationSection = authSection.Forms; DateTime now = DateTime.Now; // see http://msdn.microsoft.com/en-us/library/kybcs83h.aspx // Create a new ticket used for authentication FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 2, // Ticket version currentUser.UserName, // Username to be associated with this ticket now, // Date/time issued now.Add(formsAuthenticationSection.Timeout),// Date/time to expire createPersistentCookie, userData, FormsAuthentication.FormsCookiePath); // Hash the cookie for transport over the wire string hash = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie( FormsAuthentication.FormsCookieName, // Name of auth cookie (specified in web.config) hash); // Hashed ticket // Add the cookie to the list for outbound response HttpContext.Current.Response.Cookies.Add(cookie); } </code></pre> <p>You can use the same technique for recreating the ticket while the user is already logged in. An example is if you needed to change the <code>Ticket.UserData</code>. When issuing a new ticket you would increment the version number.</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