【发布时间】:2016-05-29 11:11:57
【问题描述】:
我正在尝试使用Cookie Middleware 身份验证创建 asp.net core mvc 6 应用程序。 我的代码编译没有错误,但即使在成功登录后我也不是授权用户
这是我的 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 也设置正确。
现在我不知道登录后我无法进入仪表板/索引。 每次由于配置设置我被重定向到 /Account/Login/
我做错了什么?
【问题讨论】:
标签: c# asp.net asp.net-core