Aspnetcore.docs: Scaffolding over existing authorization

Created on 14 Jun 2018  Â·  7Comments  Â·  Source: dotnet/AspNetCore.Docs

Authentication works, but User.IsInRole("X") does not return the expected value (it worked with previous version).
Is this expected?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P4 Source - Docs.ms

Most helpful comment

I followed instructions "Create full identity UI source" after creation of an MVC project with authorization.
When I start the application, click register, fill the form and click "register" button I got an exception : NotSupportedException: No IUserTwoFactorTokenProvider<TUser> named 'Default' is registered.

I fixed it by calling .AddDefaultTokenProviders() in the ConfigureServices method like this:

services.AddIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

All 7 comments

Ok, so in the first place I hade to add the AddRoles to Identity configuration
services.AddDefaultIdentity()
.AddRoles()
.AddEntityFrameworkStores();

Now it still doesn't work. However, it works the following construction

var user = await UserManager.GetUserAsync(User);
var roles = await UserManager.GetRolesAsync(user);

So, how come User.IsInRole does not work while UserManager.GetRolesAsync works?

Hello @alexiordan ... did you see :point_right: https://docs.microsoft.com/aspnet/core/security/authorization/roles

Yes, but what is the actual answer to my question though?

ping @HaoK

@HaoK see related #7041

Formerly one could use something like this in Account Controller:

 [HttpGet]
 [Authorize(Roles = "Master")]
 public IActionResult Register(string returnUrl = null)

How can I do the same thing with the new approach?

Its a bug in AddDefaultIdentity/AddIdentityCore, see: https://github.com/aspnet/Identity/issues/1813

I followed instructions "Create full identity UI source" after creation of an MVC project with authorization.
When I start the application, click register, fill the form and click "register" button I got an exception : NotSupportedException: No IUserTwoFactorTokenProvider<TUser> named 'Default' is registered.

I fixed it by calling .AddDefaultTokenProviders() in the ConfigureServices method like this:

services.AddIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();
Was this page helpful?
0 / 5 - 0 ratings