在ASP.NET Core 7 MVC中,自定义的SignInManager中的IsSignedIn始终为false可能是由于缺少正确的配置或代码错误导致的。以下是一些可能的解决方法:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
});
app.UseAuthentication();
[HttpPost]
[AllowAnonymous]
public async Task Login(LoginViewModel model, string returnUrl = null)
{
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
// 登录成功
return RedirectToAction("Index", "Home");
}
if (result.RequiresTwoFactor)
{
// 需要两步验证
return RedirectToAction("LoginWith2fa", new { returnUrl, model.RememberMe });
}
if (result.IsLockedOut)
{
// 账户被锁定
return RedirectToAction("Lockout");
}
else
{
// 登录失败
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return View(model);
}
}
// 登录失败
return View(model);
}
请注意,以上示例仅供参考,并且可能需要根据你的具体情况进行调整。
上一篇:ASP.NET Core 7 MVC:在页面更改时延长Cookie的持续时间
下一篇:ASP.NET Core 7 Web API - SlowCheetah 可以翻译为“ASP.NET Core 7 Web API - SlowCheetah”。