I\'ve implemented role based auth several times pre 2.1. Followed the steps to scaffold the new 2.1 identities.
I extended the IdentityUser model to add additional f
However, decorating a controller with a role will always return not authorized
[Authorize(Roles = "Administrator")]
It's a known bug in the version of 2.1
. See issue here .
I follow the advice of using the old api suggested by HaoK and C-BERBER , and it now works flawlessly .
Here's my DbContext
:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions options)
: base(options)
{
}
}
Configure the identity using the old-style api :
services.AddIdentity()
.AddRoleManager>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores();
Lastly , logout and re-signin , it will work as expected now .
I guess you won't want to debug the AuthorizeAttribe
itself , since it is processed at compile-time . If you mean to debug the AuthorizeFilter
, you can follow the steps as below :
click Tools
-> Options
-> Debugging
General
, unselect the Enable Just My Code
in Visual StudioEnable Source Link Support
Symbols
, make sure that the Microsoft Symbol Servers is selectedAnd you can debug the source code now . However , due to the way that filter works , you need set a breakpoint before MVC . I just set a dummy middleware that will take place before the MVC router handler :
The screenshot of debugging AuthorizeFiler
: