2014-01-23 109 views
1

使用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内部工作。无法检索cookie

登录页面代码:

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; 
}` 
+0

你能显示一些代码吗? – Jason

+0

您是否重定向到属于不同域的url并设置域cookie。 – Saravanan

+0

重定向仅仅是最初请求的页面。在这种情况下,它是Default.aspx。在地址栏中,当它重定向到登录页面时,它显示:http:// localhost:64432/Login?ReturnUrl =%2fDefault.aspx –

回答

1

我能够通过调整被存储在的用户数据的数据来解决我的问题的FormsAuthenticationTicket。看起来好像我试图插入的数据量超过了最大值。一旦我删除,一切都按预期工作。