【问题标题】:User.Identity.IsAuthenticated is always falseUser.Identity.IsAuthenticated 始终为假
【发布时间】:2016-04-19 04:00:40
【问题描述】:

我正在使用会员服务提供商,但如何获取我的用户名和密码来登录/登录?当我查看此代码时:

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 fails sometimes) 会导致问题,我需要避免它。

【问题讨论】:

  • 查看我上面的重复更新。我不能使用 setauthcookie
  • 使用我提供的答案,因为它不使用 SetAuthCookie。
  • 您需要确保在我的回答中提到的 Web 配置中启用了表单身份验证。不这样做是您所看到的最常见的原因。

标签: c# asp.net webforms asp.net-membership forms-authentication


【解决方案1】:

使用如下代码使其工作。 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 2012-08-20
    • 1970-01-01
    • 2019-10-21
    • 2021-03-05
    相关资源
    最近更新 更多