PasswordSignInAsync成功后,User.Identity.IsAuthenticated始终为false [英] User.Identity.IsAuthenticated always false after PasswordSignInAsync gives success
问题描述
我有一个标准的MVC项目,其中包含UserManager和SignInManager对象以及一个AccountController,并具有预先创建的登录和注册类型功能.
I've got a standard MVC project, with the UserManager and SignInManager objects and an AccountController, with the pre-created login and register type functionality.
我可以将新用户注册到我的AspNetUsers表中,但是登录时我打电话:-
I can register new users to my AspNetUsers table, but when I login I call:-
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
数据是正确地从表单传递过来的,结果是成功,这是我所期望的.
The data is coming through from the form correctly, and the result is Success, which I expected.
然后我尝试了以下重定向:-
I then tried the following redirects:-
case SignInStatus.Success:
//return RedirectToLocal("/admin/");
return RedirectToAction("Index", "Admin");
但是在任何页面上,成功登录后,User.Identity.IsAuthenticated始终为false,而User.Identity.Name为空字符串.
but on any page, after this successful login, User.Identity.IsAuthenticated is always false, and User.Identity.Name is an empty string.
我做错了什么?过去,我以相同的方式用相同的设置完成了另一个项目,而我的问题是零.
What am I doing wrong? I've done another project in the same way with the same setup in the past and I've had zero problems.
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>
有人可以建议我做错了什么吗?现在正在引起重大问题.
Can anyone suggest what I am doing wrong? It's causing major issues now.
干杯!
推荐答案
检查项目的App_Start
文件夹中是否有Startup.Auth.cs
文件.
Check to see if you have a Startup.Auth.cs
file in your App_Start
folder in the project.
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的版本和所使用的身份,您应该查看一下
Depending on the version of asp.net and identity you are using, you should take a look at this
ASP.NET Identity AuthenticationManager与SignInManager和Cookie到期
这篇关于PasswordSignInAsync成功后,User.Identity.IsAuthenticated始终为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!