2012-07-16 78 views
1

讀我想讀AUTH票的用戶名(至極是TESTTEST從驗證cookie

-----登錄頁面------

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, 
     "TESTTEST", 
     DateTime.Now, 
     DateTime.Now.AddMinutes(30), 
     false, 
     String.Empty, 
     FormsAuthentication.FormsCookiePath); 
      string encryptedTicket = FormsAuthentication.Encrypt(ticket); 
      HttpCookie authCookie = new HttpCookie(
          FormsAuthentication.FormsCookieName, 
          encryptedTicket); 
      authCookie.Secure = true; 
      Response.Cookies.Add(authCookie); 
      FormsAuthentication.RedirectFromLoginPage("User", false); 

--- ---保護頁-------

HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; 
      FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value); 
      Label1.Text = ticket.Name; 

結果:標籤文字"USER",而不是"TESTTEST"

  • 我該怎麼辦?

回答

1

FormsAuthentication.RedirectFromLoginPage使用提供的名稱(在您的情況下爲「用戶」)創建一張新票。從http://msdn.microsoft.com/en-us/library/ka5ffkce.aspx

如果CookiesSupported屬性爲true,要不就是RETURNURL變量是當前應用程序內或EnableCrossAppRedirects屬性爲true,則使用SetAuthCookie的RedirectFromLoginPage方法發出認證憑證和默認的Cookie它的地方方法。

您可能需要使用下面的代碼,而不是RedirectFromLoginPage

returnUrl = FormsAuthentication.GetRedirectUrl(userName, false); 
HttpContext.Current.Response.Redirect(returnUrl);