我正在关注 IdentityUser 的一些教程,我认为我的做法与教程中的完全一样,但是当我尝试登录时,即使 _signInManager 成功,我也会收到“MyIdentity:False”(带有 LogInformation),这似乎很奇怪。我尝试将用户设置为已确认,但没有结果。
我正在 Mac 上使用 Visual Studio。
以下是我注册用户的方法:
[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);
}
这是我尝试登录的方法:
[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);
}
以下是我为其配置服务的方法:
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();
在配置中