簡體   English   中英

User.Identity.IsAuthenticated始終為false

[英]User.Identity.IsAuthenticated is always false

我正在使用會員資格提供程序,但是如何獲取用戶名和密碼來登錄/登錄? 當我查看此代碼時:

if (User.Identity.IsAuthenticated)
// this code always returns false

用戶使用asp:Login登錄之前,我已經有了這個:

if (Membership.ValidateUser(email, password))
{
    // this is true but what am I missing here to make above not be false?

所謂的重復問題/答案使用SetAuthCookie,根據此問題( System.Web.HttpContext.Current.User.Identity.IsAuthenticated有時失敗 )會引起問題,我需要避免這種情況。

使用下面的代碼使其工作。 RedirectFromLoginPage方法將創建身份驗證cookie,並將用戶重定向到Web配置中定義的原始頁面或默認URL(即主頁)。

if (Membership.ValidateUser(email, password))
 {
    // Log the user into the site
    FormsAuthentication.RedirectFromLoginPage(email, false);
 }

另外,請確保在Web配置中啟用了表單身份驗證。 如果沒有其他設置,至少要至少set mode="Forms"

<authentication mode="Forms">
  <forms loginUrl="member_login.aspx"
    defaultUrl="index.aspx" />
</authentication>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM