Asp.NET Core 2身份验证. SignInManager.PasswordSignInAsync()成功,但是User.Identity.IsAuthenticated为false [英] Asp.NET Core 2 Authentication. SignInManager.PasswordSignInAsync() succeeds, but User.Identity.IsAuthenticated is false
问题描述
我正在关注IdentityUser上的一些教程,我认为我的操作与教程中的完全相同,但是当我尝试登录时,即使您_signInManager成功,我也会收到"MyIdentity:False"(使用LogInformation),这似乎很奇怪.我试图将用户设置为已确认,但没有结果.
I was following some tutorials on IdentityUser, and I think I did exactly as in tutorials, but when I try to login, I get "MyIdentity: False" (with LogInformation) even thou _signInManager succeeds, which seems to be very weird. I tried to set the users as confirmed, but with no result.
我正在Mac上使用Visual Studio.
I'm working on Visual Studio on Mac.
这是我注册用户的方式:
Here's how I register my users:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser()
{
UserName = model.Email,
Email = model.Email
};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, false);
return this.RedirectToAction("Index", "Home");
}
else
{
foreach (var error in result.Errors)
ModelState.AddModelError("", error.Description);
}
}
return View(model);
}
这是我尝试登录的方式:
And here's how I try to login:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(
model.Email,
model.Password,
model.RememberMe,
lockoutOnFailure: false
);
if (result.Succeeded)
{
_logger.LogInformation("MyIdentity: " + User.Identity.IsAuthenticated);
return this.RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", "Invalid Login attempt.");
}
return View(model);
}
这是我为其配置服务的方式:
Here's how I configure the services for it:
services.AddDbContext<CmagruDBContext>(options =>
options.UseSqlite("Data Source=Cmagru.db",
b => b.MigrationsAssembly("Presentation")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<CmagruDBContext>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 3;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.User.RequireUniqueEmail = true;
});
services.ConfigureApplicationCookie(options =>
{
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
options.LoginPath = "/Account/Login";
options.AccessDeniedPath = "/Account/AccessDenied";
options.SlidingExpiration = true;
});
我在配置中确实有app.UseAuthentication();
推荐答案
对于其他可能遇到此问题的人:
就我而言,我只需要在Configure()
中的app.UseMvc()
之前放置app.UseAuthentication();
.否则,它对我不起作用.
For other people who may encounter this problem:
In my case, I had to simply put app.UseAuthentication();
before app.UseMvc()
in Configure()
. Otherwise, it doesn't work for me.
我想我在文档中错过了.
I think I missed that in documentation.
这篇关于Asp.NET Core 2身份验证. SignInManager.PasswordSignInAsync()成功,但是User.Identity.IsAuthenticated为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!