我正在尝试使用Cookie中间件身份验证创建asp.net core mvc 6应用程序。我的代码编译没有错误,但即使成功登录,我也没有被授权用户。
这是我的startup.cs配置:
这是我的startup.cs配置:
app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = "CookieAuth";
options.LoginPath = new PathString("/Account/Login/");
options.AccessDeniedPath = new PathString("/Account/Login/");
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
});
还有我的控制器中的登录操作:
public async Task<IActionResult> Login(LoginViewModel model)
{
User foundUser = _userManager.findUser(model.UserName, model.Password);
if (foundUser != null)
{
List<Claim> userClaims = new List<Claim>
{
new Claim("userId", Convert.ToString(foundUser.UserID)),
new Claim(ClaimTypes.Name, foundUser.UserName),
new Claim(ClaimTypes.Role, Convert.ToString(foundUser.RoleID))
};
ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(userClaims));
await HttpContext.Authentication.SignInAsync("CookieAuth", principal);
return RedirectToAction("Index", "Dashboard");
}
return View();
}
最后是仪表板/索引操作
[Authorize]
public IActionResult Index()
{
return View();
}
我在登录操作中设置了一些断点,一切似乎都正常。 Cookie也设置正确。
现在我不知道为什么我无法在登录后进入仪表板/索引页面。 每次都由于配置设置而被重定向到/账户/登录/
我做错了什么?
authenticationType
参数应该设置为什么?是“Cookie”,“JWT”还是使用的方案或其他任何东西? - Omar