首页 > 解决方案 > 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.

enter image description here 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

标签: c#asp.net-coreasp.net-core-identity

解决方案


推荐阅读