FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,username,DateTime.Now,DateTime.Now.AddMinutes(Timeout)false};string encryptedTicket = FormsAuthentication.Encrypt(authTicket);httpcookie authcookie = new httpcookie(FormsAuthentication.Formscookiename,encryptedTicket); httpContext.Current.Response.cookies.Add(authcookie);
如何获取添加的cookie和用户详细信息(authTicket)?
解决方法 您可以使用类似于以下内容的代码检索FormsAuthenticationTicket
: // RetrIEves the cookie that contains your custom FormsAuthenticationTicket.httpcookie authcookie = httpContext.Current.Request.cookies[FormsAuthentication.Formscookiename];// Decrypts the FormsAuthenticationTicket that is held in the cookie's .Value property.FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authcookie.Value);// The "authTicket" variable Now contains your original,custom FormsAuthenticationTicket,// complete with User-specific custom data. You can then check that the FormsAuthenticationTicket's// .name property is for the correct user,and perform the relevant functions with the ticket.// Here,we simply write the user-specific data to the http Response stream.if (authTicket.name == txtUsername.Text){ Response.Write(authTicket.UserData);}
上面的代码引用了txtUsername.Text之类的东西,所以这里有一个完整的.ASPX页面,您可以将其粘贴到一个空的ASP.NET webform中以查看它是如何工作的:
<%@ 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.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 { // RetrIEves the cookie that contains your custom FormsAuthenticationTicket. httpcookie authcookie = httpContext.Current.Request.cookies[FormsAuthentication.Formscookiename]; // Decrypts the FormsAuthenticationTicket that is held in the cookie's .Value property. FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authcookie.Value); // The "authTicket" variable Now contains your original,// complete with User-specific custom data. You can then check that the FormsAuthenticationTicket's // .name property is for the correct user,and perform the relevant functions with the ticket. // Here,we simply write the user-specific data to the http Response stream. if (authTicket.name == txtUsername.Text) { Response.Write(authTicket.UserData); } } } </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>总结
以上是内存溢出为你收集整理的c# – 访问表单身份验证票证全部内容,希望文章能够帮你解决c# – 访问表单身份验证票证所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)