1

我有两个页面 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。

谢谢!

4

1 回答 1

0

我会FormsAuthentication.FormsCookieName用来设置cookie名称。

cookie.Name = FormsAuthentication.FormsCookieName;

对我来说,这设置为.ASPXAUTHnot .AUTH

于 2013-03-11T18:58:37.593 回答