我有两页login.aspx和cookie.aspx
当用户登录时,假设创建一个cookie并重定向到cookie.aspx。当cookie.aspx加载时,它会检查cookie是否存在。
这是我的login.aspx登录代码
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
userName = Login1.UserName;
passWord = Login1.Password;
athUser = DirectoryServicesProcessor.Verify_User(userName, passWord);
if (athUser == true)
{
//FormsAuthentication.SetAuthCookie(userName, false);
bool IsPersistentCookie = true;
DateTime expirationDate = new DateTime();
if (IsPersistentCookie)
expirationDate = DateTime.Now.AddYears(1);
else
expirationDate = DateTime.Now.AddMinutes(300);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
userName,
DateTime.Now,
expirationDate,
IsPersistentCookie,
userName,
FormsAuthentication.FormsCookiePath);
string et = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, et);
if (IsPersistentCookie)
cookie.Expires = ticket.Expiration;
cookie.Name = ".AUTH";
cookie.Domain = ".mydomain.com";
Response.SetCookie(cookie);
Response.Cookies.Add(cookie);
Response.Redirect("~/cookietest/cookie.aspx");
}
这是我的cookie.aspx的页面加载
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName);
if (cookie != null)
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
if (ticket != null)
{
WhatHappened.Text = "Found the cookie!";
}
else
{
Response.Redirect("../failure.aspx");
}
}
else
{
Response.Redirect("../failure.aspx");
}
}
我想知道为什么cookie.aspx找不到在login.aspx中创建的cookie。
谢谢!
答案 0 :(得分:0)
我会使用FormsAuthentication.FormsCookieName
来设置Cookie名称。
cookie.Name = FormsAuthentication.FormsCookieName;
对我而言,设置为.ASPXAUTH
而不是.AUTH
。