asp.net core - In JWT Authorization, check if user has role Admin -


i working on .net core api, , inside controller, have following code:

if (user.identity.isauthenticated) {     var username = httpcontext.user.findfirstvalue(claimtypes.nameidentifier);     var user = await _usermanager.findbynameasync(username);     artistcarddtocollection = _artistsservice.getallartists(user.id, user.isinrole("admin")); } 

the code above because wish pass user.id (if logged in) , isadmin flag getallartists method.

the code above failing on user.isinrole("admin"). false when know 100% user in question admin. i've double checked database via sql management studio.

this makes me think 1 can't use user.isinrole() when working jwt. if case, correct way? thanks

probably caching issue user.isinrole(), if check documentation find:

isinrole first checks isrolelistcached property determine whether cached list of role names current user available. if isrolelistcached property true, cached list checked specified role. if isinrole method finds specified role in cached list, returns true. if isinrole not find specified role, calls getrolesforuser method of default provider instance determine whether user name associated role data source configured applicationname value.

in case can try use getrolesasync below:

var user = await _usermanager.findbynameasync(username); var roles = await _usermanager.getrolesasync(user); artistcarddtocollection = _artistsservice.getallartists(user.id, roles.contains("admin")); 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -