You can get FormsAuthenticationTicket using code similar to the following:
The code above contains a link to things like txtUserName.Text , so here is the full .ASPX page that you can insert into an empty ASP.NET web form to see how it works:
<%@ Page Language="C#" %> <%@ Import Namespace="System.Web.Security" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { double Timeout = 15.00; if (!IsPostBack) { FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,txtUserName.Text, DateTime.Now,DateTime.Now.AddMinutes(Timeout), false, "This is my secret user-specific data"); string encryptedTicket = FormsAuthentication.Encrypt(authTicket); HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket); HttpContext.Current.Response.Cookies.Add(authCookie); } else { </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Forms Authentication Login</title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td> UserName: </td> <td> <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Password: </td> <td> <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Button ID="Button1" runat="server" Text="Login" /> </td> </tr> </table> </div> </form> </body> </html>
Craigtp
source share