【问题标题】:Avoid manual setting of FormsAuthentication cookie避免手动设置 FormsAuthentication cookie
【发布时间】:2016-01-14 00:24:51
【问题描述】:

我在使用这个的不同时间都有不良行为:

FormsAuthentication.SetAuthCookie(user.UserName, true);

.Net 将如何/将如何设置 cookie?

我试过这个:(System.Web.HttpContext.Current.User.Identity.IsAuthenticated fails sometimes)

但我的 User.Identity.IsAuthenticated 总是假的

什么给了?

【问题讨论】:

  • SetAuthCookie 不起作用的原因是您没有在 Web 配置中启用表单身份验证。在 system.web 标签内的 web 配置中包含以下设置:<authentication mode= "Forms" />
  • 我一直都有,而且大部分时间都有效
  • 请记住,您也可以通过简单地调用FormsAuthentication.RedirectFromLoginPage 方法来设置cookie,该方法将设置身份验证票证/cookie,并将使用重定向到请求的URL或默认URL。
  • 是的,我做到了。这样就不手动设置cookie,然后自己设置了?
  • 是的,该方法封装了cookie创建功能。

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


【解决方案1】:

首先确保在您的 Web 配置文件中启用了表单身份验证。

<authentication mode="Forms" />

使用如下代码使其工作。 RedirectFromLoginPage 方法将创建身份验证 cookie 并将用户重定向到原始页面或默认 URL,即主页。

if (Membership.ValidateUser(userName, password))//or whatever method you use
 {
    // Log the user into the site
    FormsAuthentication.RedirectFromLoginPage(userName, false);
 }

【讨论】:

    猜你喜欢
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多