Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty Roles #12

Open
jaredcnance opened this issue May 31, 2016 · 4 comments
Open

Empty Roles #12

jaredcnance opened this issue May 31, 2016 · 4 comments

Comments

@jaredcnance
Copy link

jaredcnance commented May 31, 2016

For some reason User.Roles is empty when it reaches the controller and throws a 403 for role based authorization.

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IDatabaseInitializer databaseInitializer, UserManager<ApplicationUser> userManager)
{
...
app.UseOpenIddictCore(...);
app.UseJwtBearerAuthentication(new JwtBearerOptions() {...});
app.Use(async (context, next) =>
      {
        var user = await userManager.GetUserAsync(context.User);
//-------> user.Roles.Count = 1
        await next.Invoke();
      });
app.UseMvc(...);
...
}

But, in the controller:

[Authorize]
  public class MeController : Controller
  {
    private CoreModelsDbContext _context;
    private readonly UserManager<ApplicationUser> _userManager;

    public MeController(CoreModelsDbContext context, UserManager<ApplicationUser> userManager)
    {
      _context = context;
      _userManager = userManager;
    }

    [Route("api/v1/me"), HttpGet]
    public async Task<IActionResult> Get()
    {
      var user = await _userManager.GetUserAsync(User);
//-------> user.Roles.Count = 0
      return user == null ? Ok("No user / not logged in") : Ok(user);
    }
  }

Any idea how this would happen?

@snowping
Copy link

snowping commented Jun 7, 2016

I've got the same issue. Any hint regarding "permission denied 403" error is highly appreciated. With the same controller role based authentication works fine using cookie authentication.

Update: Ah I am sorry, I just found out. I missed the scope "roles" in my json payload! Now everything is working. @jaredcnance btw: the roles property is empty because there is no support for lazy loading in ef7 as of yet. You need to load the roles explicitly:

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

return Ok(new { User = user, Roles = roles});

@DidierVanegas
Copy link

Hi @snowping , you can explain me where the change is made. I am noob. Thank you.

@snowping
Copy link

snowping commented Jul 2, 2016

@DidierVanegas sure, in order to get a proper token (including roles) you need to send x-www-form-urlencoded params to a URL e.g. http://localhost:5000/connect/token. Enclosed you'll find a postman example how to generate a token including "roles" in scope parameter.

image

Hope it'll work out.

@DidierVanegas
Copy link

@snowping thank you very much, it works now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants