2015-01-08 2 views
-1

이 경우에만 Web Forms Application 프로젝트를 만들었습니다. 프레임 워크의 기본 웹 템플리트를 사용하고 있습니다. Default.aspx 페이지에 3 개의 버튼과 1 개의 레이블이 수정되어 있습니다.올바르게 암호화 된 FormsAuthentication 쿠키를 설정하고 검색하는 방법

버튼 :btnLogin, btnSetCookie, btnGetCookie

레이블 :lblCookieInfo

흐름 :

  1. 로그인을 클릭
  2. 클릭 Set Cookie 버튼
  3. 클릭 Decrypt 방법 (Invalid value for 'encryptedTicket' parameter)에 도달 할 때

지금, 내가 쿠키를 검색하는 세 번째 버튼을 클릭하면, 항상 나에게 오류가 발생 Get Cookie 버튼을 클릭합니다. httpCookie에 쿠키를 검색하려고하면 아무 값도없이 공백입니다. 내가 뭘 잘못 했니?

protected void btnLogin_Click(object sender, EventArgs e) 
{ 
    FormsAuthentication.SetAuthCookie("myUserName", createPersistentCookie: true); 
    Response.Redirect("~/"); 
} 

protected void btnSetCookie_Click(object sender, EventArgs e) 
{ 
    var ticket = new FormsAuthenticationTicket(1, 
     "myUserName", 
     DateTime.Now, 
     DateTime.Now.AddMinutes(10), 
     true, 
     "data value of cookie", 
     FormsAuthentication.FormsCookiePath); 

    string encTicket = FormsAuthentication.Encrypt(ticket); 

    var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket) 
    { 
     Expires = ticket.Expiration, 
     HttpOnly = true 
    }; 
    btnGetCookie.Enabled = true; 

    Response.Cookies.Add(authCookie); 
} 

protected void btnGetCookie_Click(object sender, EventArgs e) 
{ 
    var httpCookie = Response.Cookies[FormsAuthentication.FormsCookieName]; 
    lblCookieInfo.Visible = true; 
    if (httpCookie == null) 
    { 
     lblCookieInfo.Text = "Cookie is Null"; 
     return; 
    } 

    //Here throws error! 
    var decryptedCookie = FormsAuthentication.Decrypt(httpCookie.Value); 
    if (decryptedCookie == null) 
    { 
     lblCookieInfo.Text = "Cookie can't be decrypted."; 
     return; 
    } 

    lblCookieInfo.Text = string.Format("Name: {0}, Is Expired: {1}, Is Persistent: {2}, Expiration: {3}, Path: {4}, User data: {5}", 
     decryptedCookie.Name, decryptedCookie.Expired, 
     decryptedCookie.IsPersistent, decryptedCookie.Expiration, 
     decryptedCookie.CookiePath, decryptedCookie.UserData); 
} 
+0

왜 예외가 발생했는지 확인할 수 있었습니까? 나는 지금 당장 비슷한 문제를 겪고있다. 이 코드는 여기에 언급 된 바와 같이 빈 쿠키 값을 확인하지 않음을 알았습니다 : http://stackoverflow.com/questions/18895746/invalid-value-for-encryptedticket-parameter/24837242#24837242 도움이 될지 모르겠지만 여전히 호기심이 왜 비어 있을까? –

+0

@mklinker 지금 당장은 기억이 나지 않지만 그 문제는 엄청나게 간단하다는 것을 기억합니다. 당신이 말하는대로 될 가능성이 있습니다. – CodeArtist

+0

@mklinker 내 대답을 한번보세요. 당신을 도울 수 있습니다. – CodeArtist

답변

1

정말 어떻게 해결합니까 기억이 안나지만 다음과 같은 클래스를 만들었습니다. 문제는 FormsAuthenticationTicket(...) 함수의 매개 변수라고 생각합니다.

public static class EncryptedCookie 
{ 
    public static HttpCookie SetEncryptedCookie(string name, DateTime expiration, bool httpOnly, string userData, string cookiePath) 
    { 
     var ticket = new FormsAuthenticationTicket(1, name, DateTime.Now, expiration, false, userData, cookiePath); 
     string encTicket = FormsAuthentication.Encrypt(ticket); 

     var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket) 
     { 
      Expires = ticket.Expiration, 
      HttpOnly = httpOnly 
     }; 
     return authCookie; 
    } 

    public static FormsAuthenticationTicket GetEncryptedCookie(HttpCookie cookie) 
    { 
     if (cookie == null || string.IsNullOrEmpty(cookie.Value)) return null; 
     FormsAuthenticationTicket decryptedCookie; 
     try 
     { 
      decryptedCookie = FormsAuthentication.Decrypt(cookie.Value); 
      if (decryptedCookie == null || string.IsNullOrEmpty(decryptedCookie.Name) || decryptedCookie.Expired) return null; 
     } 
     catch 
     { 
      return null; 
     } 
     return decryptedCookie; 
    } 

    public static void RemoveCookie(string cookieName) 
    { 
     HttpContext.Current.Request.Cookies.Remove(cookieName); 
    } 
} 
관련 문제