Kub*_*uba 15 c# asp.net asp.net-core
我正在尝试使用Cookie中间件身份验证创建asp.net核心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;
});
Run Code Online (Sandbox Code Playgroud)
还在我的控制器中登录操作:
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();
}
Run Code Online (Sandbox Code Playgroud)
最后是Dashboard/Index动作
[Authorize]
public IActionResult Index()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
我在登录操作中放了一些断点,一切似乎都运行正常.Cookie也正确设置.
现在我不知道登录后我不能去仪表板/索引.每次我被重定向到/帐户/登录/由于配置设置
我究竟做错了什么 ?
Jam*_*tan 17
ClaimsIdentity
在登录中构建时,需要使用指定的不同构造函数authenticationType
.
代替
ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(userClaims));
Run Code Online (Sandbox Code Playgroud)
你应该做:
ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(userClaims, "local"));
Run Code Online (Sandbox Code Playgroud)
现在可以创建具有声明的ClaimsIdentity,但将IsAuthenticated设置为false.实际上这是默认的......
要将IsAuthenticated设置为true,您需要指定身份验证类型
我从Dominick Baier的博客那里得到了这个信息.
也有使用cookie中间件的一个很好的例子,在这里,也被(传奇)多米尼克·拜尔/ leastprivilege.
编辑:
此答案包含有关字符串应使用的内容的更多信息authenticationType
.
归档时间: |
|
查看次数: |
5522 次 |
最近记录: |