无法检索cookie

时间:2014-01-23 20:41:27

标签: c# cookies forms-authentication

使用FormsAuthentication,我正在创建FormsAuthenticationTicket,加密,使用Response.Cookies.Add(authCookie)将其添加到cookie。然后,我使用Response.Redirect重定向到请求的原始页面。 Application_AuthenticateRequest方法中的Global.asax中有代码,用于检索cookie - HttpCookie authCookie = Context.Request.Cookies [cookieName]。但是,出于某种原因,在调用重定向后它遇到Global.asax代码时,集合中没有cookie。在这一点上,我有点难过为什么它从集合中丢失了cookie。有关为什么会发生这种情况的任何想法?现在,我只是在localhost中工作。

登录页码:

    string adPath = "LDAP://ldapserveraddress";

    LdapAuthentication adAuth = new LdapAuthentication(adPath);
    try
    {
        if (true == adAuth.IsAuthenticated("ES", txtUsername.Text, txtPassword.Text))
        {
            string groups = adAuth.GetGroups();


            //Create the ticket, and add the groups.
            bool isCookiePersistent = chkPersist.Checked;
            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
                      txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(60), isCookiePersistent, groups);

            //Encrypt the ticket.
            string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

            //Create a cookie, and then add the encrypted ticket to the cookie as data.
            HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

            if (true == isCookiePersistent)
                authCookie.Expires = authTicket.Expiration;

            //Add the cookie to the outgoing cookies collection.
            Response.Cookies.Add(authCookie);

            string redirect = FormsAuthentication.GetRedirectUrl(txtUsername.Text, false);
            //You can redirect now.
            Response.Redirect(redirect,false);
        }
        else
        {
            errorLabel.Text = "Authentication did not succeed. Check user name and password.";
        }
    }
    catch (Exception ex)
    {
        errorLabel.Text = "Error authenticating. " + ex.Message;
    }
}

Global.asax代码(Application_AuthenticateRequest):

    string cookieName = FormsAuthentication.FormsCookieName;
    HttpCookie authCookie = Context.Request.Cookies[cookieName];

    if (null == authCookie)
    {
        //There is no authentication cookie.
        return;
    }
    FormsAuthenticationTicket authTicket = null;
    try
    {
        authTicket = FormsAuthentication.Decrypt(authCookie.Value);
    }
    catch (Exception ex)
    {
        //Write the exception to the Event Log.
        return;
    }
    if (null == authTicket)
    {
        //Cookie failed to decrypt.
        return;
    }
    //When the ticket was created, the UserData property was assigned a
    //pipe-delimited string of group names.
    string[] groups = authTicket.UserData.Split(new char[] { '|' });
    //Create an Identity.
    GenericIdentity id = new GenericIdentity(authTicket.Name, "LdapAuthentication");
    //This principal flows throughout the request.
    GenericPrincipal principal = new GenericPrincipal(id, groups);
    Context.User = principal;
}`

1 个答案:

答案 0 :(得分:1)

我能够通过调整存储在FormsAuthenticationTicket的userData中的数据来解决我的问题。似乎我尝试插入的数据量超过了最大值。一旦我删除,一切都按预期工作。