2009-12-28 56 views
1

我正在使用VSTS 2008 + C#+ .Net 3.5 + ASP.Net + IIS 7.0。我正在實施表單身份驗證。表單身份驗證問題

我想知道在Forms身份驗證中,如何檢查用戶是否已經過身份驗證?

回答

7

您可以使用HttpContext.Current.User.Identity.IsAuthenticated來檢查它們是否已通過身份驗證。例如

if(User.Identity.IsAuthenticated) 
{ 
Response.Write("Logged in already"); 
} 
else 
{ 
Response.Write("Please log in"); 
} 
+0

當User.Identity將被設置?在我目前的代碼中,我只是調用「FormsAuthentication.SetAuthCookie(someusernumber,false)」,這個方法是否會設置User.Identity? – George2

+2

是的,它會因爲SetAuthCookie認證用戶。設置Cookie後,您可能需要重新加載頁面。在SetAuthCookie之後檢查身份驗證將返回true。 – keyboardP

+0

謝謝,問題回答! – George2