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

How to handle multiple roles for same user and Authorize an api #109

Closed
Munde opened this issue Nov 5, 2018 · 8 comments
Closed

How to handle multiple roles for same user and Authorize an api #109

Munde opened this issue Nov 5, 2018 · 8 comments
Assignees
Labels
question Further information is requested

Comments

@Munde
Copy link

Munde commented Nov 5, 2018

Hello Skoruba ,
Thanks for this nice project it helps a lot

But am having two issues

  1. is how can allow multiple roles for a single user , this comes i hand when i have more than one application where user can be able to share the login i tried to implement but it ends up in access denied page failing to authorize the user

  2. what are the best way to authorize the API, i tried to implement but it works only on first minute after first minute the API loses access hence preventing user to have access to an API and the refresh token is invalid and throws an error when i try to renew the access token using the refresh token

@skoruba
Copy link
Owner

skoruba commented Nov 6, 2018

Hey @Munde,
1.) Have you inspect the token - if the token contains a list of roles as you requested from IS4? Btw: did you mean - add more roles to single user via view - /Identity/UserRoles?
2.) I can highly recommend take a look at Samples of IdentityServer4 - there are a lot of great example how to work with API.

@skoruba skoruba closed this as completed Nov 6, 2018
@skoruba skoruba reopened this Nov 6, 2018
@skoruba
Copy link
Owner

skoruba commented Nov 6, 2018

Here: https://github.com/IdentityServer/IdentityServer4.Samples
Sorry - I accidentally closed it from my mobile. :))

@skoruba skoruba closed this as completed Nov 6, 2018
@skoruba skoruba reopened this Nov 6, 2018
@skoruba skoruba self-assigned this Nov 6, 2018
@skoruba skoruba added the question Further information is requested label Nov 6, 2018
@Munde
Copy link
Author

Munde commented Nov 6, 2018

yes i mean multiple roles via identity/UserRoles yes like a may posses two or more roles and i can authorize using one or more roles in the application,
like
[Authorize(Roles="SkorubaAdministrator,SuperAdmin,Customer")]
when i assign more than one role to the user i end up getting access denied
roles
like this when i assign multiple roles it only first role n discard other role

@skoruba
Copy link
Owner

skoruba commented Nov 6, 2018

Could you please check the User object and claims - if the roles are part of these claims?
Does your client ask for roles in scope?
Thanks!

@skoruba
Copy link
Owner

skoruba commented Nov 6, 2018

Btw: take a look here IdentityServer/IdentityServer4#1786 (comment)

@Munde
Copy link
Author

Munde commented Nov 7, 2018

Yes my client ask role as a part of its scope, when i do assign single role it works as expected but when i add multiple roles to same user i end up i getting this error
internal server error

and this are the setting in my AddOpenId
capture

i tried both using
options.ClaimActions.MapUniqueJsonKey("role", "role","roles");
and options.ClaimActions.MapUniqueJsonKey("role", "role","role"); still i get the above error

@skoruba
Copy link
Owner

skoruba commented Nov 7, 2018

OK, I will check it later.
Thanks for reporting

@Munde
Copy link
Author

Munde commented Nov 26, 2018

We have been able to solve it by adding this piece of code on option.Events
OnUserInformationReceived = async context => { if (context.User.TryGetValue(JwtClaimTypes.Role, out JToken role)) { var claims = new List<Claim>(); if (role.Type != JTokenType.Array) { claims.Add(new Claim(JwtClaimTypes.Role, (string)role)); } else { foreach (var r in role) claims.Add(new Claim(JwtClaimTypes.Role, (string)r)); } var id = context.Principal.Identity as ClaimsIdentity; id.AddClaims(claims); } }

@Munde Munde closed this as completed Nov 26, 2018
@Munde Munde reopened this Nov 27, 2018
@Munde Munde closed this as completed Jan 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants