避免手动设置 FormsAuthentication cookie

Avoid manual setting of FormsAuthentication cookie

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

FormsAuthentication.SetAuthCookie(user.UserName, true);

.Net 如何 does/will 设置 cookie?

我已经试过了:()

但是我的User.Identity.IsAuthenticated总是错误的

什么给了?

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

<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);
 }