c# - issues with Identity and Authorize using Roles and Policy
问题描述
it's my first time implementing identity and are having issues with Roles. I've created two roles (User and SuperUser). On startup I define to policy's (Basic, Super).
In the database the user has the correct Role, but when I do User.IsInRole("User") I get false.
Also after sign in the class attribute [Authorize] causes a redirect to Sign In.
A cookie is created, so it signs in ok.
Is there any other part I need to configure?
opt.Authorization(optn =>
{
optn.AddPolicy("Basic", p =>
{
p.RequireRole("User");
});
optn.AddPolicy("Super", p =>
{
p.RequireRole("User");
p.RequireRole("SuperUser");
});
});
[HttpPost]
public async Task<IActionResult> SignIn([FromForm] string email, [FromForm] string password)
{
var tmp = await this.SignInManager.PasswordSignInAsync(email, password, false, false);
string url = tmp.Succeeded ? Url.Action("Index", "Secure") : Url.Action("Index", "Assets", new { Error = 1 });
return RedirectPermanent(url);
}
Update I was calling app.UseAuthentication() in wrong order. When running UseAuthentication before UseMVC it works
解决方案
推荐阅读
- python - Pandas 数据框读取 csv 给出了值中的隐藏字符
- c# - 惰性初始化(惰性
) 或带有支持字段的属性 - php - 我应该在应用层服务中测试什么
- python - 显示文件的功能不起作用 - Python
- pytorch - 如何在 pytorch 中为 BERT 的训练器准备训练数据集?
- github - GitHub 帐户和存储库
- html - Powershell脚本修复ConvertTo-Html的输出
- kotlin - 编译器显示错误 Kotlin: Variable 'mAdapter' must be initialized
- powershell - PowerShell:将 HTML 附加到具有多个值的属性
- reactjs - 错误发生意外错误:“https://registry.yarnpkg.com/eslint-config-pretttier:未找到”