gpt4 book ai didi

asp.net-mvc - PasswordSignInAsync 成功后 User.Identity.IsAuthenticated 始终为 false

转载 作者:行者123 更新时间:2023-12-01 23:00:51 25 4
gpt4 key购买 nike

我有一个标准的 MVC 项目,其中包含 UserManager 和 SignInManager 对象以及 AccountController,以及预先创建的登录和注册类型功能。

我可以向我的 AspNetUsers 表注册新用户,但是当我登录时,我会调用:-

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

数据正确地从表单中传递出来,结果是成功,这正是我所期望的。

然后我尝试了以下重定向:-

case SignInStatus.Success:
//return RedirectToLocal("/admin/");
return RedirectToAction("Index", "Admin");

但在任何页面上,登录成功后,User.Identity.IsAuthenticated 始终为 false,而 User.Identity.Name 为空字符串。

我做错了什么?我过去以相同的方式和相同的设置完成了另一个项目,并且我遇到了零问题。

web.config

<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />
<!--<authentication mode="Forms">
<forms loginUrl="~/Account/Login/" timeout="1000" />
</authentication>-->
<authentication mode="None" />
</system.web>
<modules>
<remove name="FormsAuthentication" />
</modules>

任何人都可以建议我做错了什么吗?它现在引起了重大问题。

干杯!

最佳答案

检查项目的 App_Start 文件夹中是否有 Startup.Auth.cs 文件。

public partial class Startup {
public void ConfigureAuth(IAppBuilder app) {
// This uses cookie to store information for the signed in user
var authOptions = new CookieAuthenticationOptions {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
LogoutPath = new PathString("/Account/Logout"),
ExpireTimeSpan = TimeSpan.FromDays(7),
};
app.UseCookieAuthentication(authOptions);
}
}

并从 Startup 类调用

public partial class Startup {
public void Configuration(IAppBuilder app) {
// Surface Identity provider
ConfigureAuth(app);

//..other start up code
}
}

根据您使用的 asp.net 版本和身份,您应该查看此内容

ASP.NET Identity AuthenticationManager vs. SignInManager and cookie expiration

关于asp.net-mvc - PasswordSignInAsync 成功后 User.Identity.IsAuthenticated 始终为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36366059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com