會話是不檢查用戶是否被認證或不是這樣。清除應用程序池時,管理員可以根據需要清除會話,也可以通過服務器上的低內存清除會話。在這種情況下,您不會希望註銷用戶。在ASP.NET中這樣做的內建和推薦的方法是將數據存儲在身份驗證cookie中。一旦用戶登錄,您發出包含所有數據的cookie,包括用戶ID,名稱等。然後,您不必檢查會話中的每個屬性是否爲null,更簡單 - 您只需檢查用戶通過身份驗證 - 那麼你已經獲得了數據,否則 - 沒有。和其他利益,如果你替換內置主要以自定義的,你可以定義強類型的對象,它擁有從會話對象中提取用戶數據,沒有更多的鑄造。我這裏還有例子定義與窗體身份驗證
首先定義主體,讓我們來定義自定義MyIdentity和MyPrincipal
public class MyIdentity : IIdentity
{
private FormsAuthenticationTicket _Ticket;
private int _userId = 0;
public FormsAuthenticationTicket Ticket
{
get { return _Ticket; }
}
public string Name
{
get { return _Ticket.Name; }
}
public int UserId
{
get
{
if (_userId == 0)
_userId = Convert.ToInt32(_Ticket.UserData.Split("|".ToCharArray())[0]);
return _userId;
}
}
public Identity(FormsAuthenticationTicket ticket)
{
this._Ticket = ticket;
}
public string AuthenticationType
{
get { return "Custom"; }
}
public bool IsAuthenticated
{
get { return UserId > 0; }
}
}
然後保存MyIdentity
public class MyPrincipal : IPrincipal
{
private MyIdentity _Identity;
public Principal(MyIdentity identity)
{
_Identity = identity;
}
public IIdentity Identity
{
get { return _Identity; }
}
public bool IsInRole(string role)
{
return false;
}
}
則替換原有形式的用戶與MyPrincipal自定義的一個。 Global.asax
private void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
{
IPrincipal usr = HttpContext.Current.User;
// If we are dealing with an authenticated forms authentication request
if (usr.Identity.IsAuthenticated && usr.Identity.AuthenticationType == "Forms")
{
FormsIdentity formsIdentity = usr.Identity as FormsIdentity;
// Create a CustomIdentity based on the FormsAuthenticationTicket
IIdentity identity = new MyIdentity(formsIdentity.Ticket);
IPrincipal principal = new MyPrincipal(identity);
// Attach the CustomPrincipal to HttpContext.User and Thread.CurrentPrincipal
HttpContext.Current.User = principal;
Thread.CurrentPrincipal = principal;
}
}
定義發佈表單認證票證的方法。稍後,自定義MyIdentity類將從userData中提取userId和其他方法。
public static HttpCookie GetAuthCookie(string userName, string userData, bool createPersistentCookie, HttpSessionStateBase session)
{
HttpCookie authCookie = FormsAuthentication.GetAuthCookie(userName, createPersistentCookie);
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, userData, session.SessionID);
authCookie.Value = FormsAuthentication.Encrypt(newTicket);
return authCookie;
}
當用戶檢查並進行身份驗證,返回他們的身份驗證cookie
Response.Cookies.Add(AuthenticationCookie.GetAuthCookie(model.UserName, GetUserInfo(model.UserName, passwordHash), model.RememberMe, Session));
//GetUserInfo returns | separated string of user datas. "userId|userName|firstName|lastName" for example.
,最後,利用上述所有的代碼
if(User.Identity.IsAuthenticated)
{
int userId = ((MyIdentity)User.Identity).UserId;
}
這看起來可能較大代碼,但是在運行時,它將比存儲會話中的所有數據帶來更多好處。它們的主要部分是每次都進行空值檢查和投射。
這聽起來您能不能給會議的一個實例對象 – StevieB
嘿,如果我保存這個會議(「當前用戶」)對象如何我可以檢索即變量名從它? I.e Session(「CurrentUser」)。用戶名?此外,我同意檢查在母版頁空檢查,但我發現,如果我不把我的用戶控件空校驗,即和呼叫會話變量在那裏,如果會話已經死了,我得到零點異常,怎麼來的主頁不能捕捉到? – StevieB
你可能做的檢查主控頁面下旬在你的頁面生命週期。如果您的用戶已被登錄,您可以在構建子控件之前直接重定向。在這裏看到:http://stackoverflow.com/questions/5593652/asp-net-page-page-load-firing-before-master-pages-page-load-event – thekip