2013-09-23 88 views
1

我爲我的網站使用自定義會員供應商。並使用此代碼登錄:如何在自定義會員供應商中保存ASP.NET_SessionId

private User SetupFormsAuthTicket(string userName, bool persistanceFlag) 
    { 
     User user; 
     UsersContext usersContext = new UsersContext(); 
      user = usersContext.GetUser(userName); 

     var userId = user.UserId; 
     var userData = userId.ToString(CultureInfo.InvariantCulture); 
     var authTicket = new FormsAuthenticationTicket(1, //version 
          userName, // user name 
          DateTime.Now,    //creation 
          DateTime.Now.AddMinutes(30), //Expiration 
          persistanceFlag, //Persistent 
          userData); 

     var encTicket = FormsAuthentication.Encrypt(authTicket); 
     Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); 
     return user; 
    } 

但我檢查的cookie在瀏覽器中,只需保存ASPXAUTH,而不是保存ASP.NET_SessionId

我想保存ASP.NET_SessionId但persistanceFlag是真實的。

回答

0

如果要保留認證憑證,則需要明確地將Cookie Expires設置爲與認證憑證到期時間相同。

.... 

var encTicket = FormsAuthentication.Encrypt(authTicket); 
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); 

if (authTicket.IsPersistent) 
{ 
    cookie.Expires = authTicket.Expiration; 
} 

Response.Cookies.Add(cookie);