c# - Get ClaimsIdentity during OWIN middleware when issuing new access token -
i've implemented oauth 2 authentication in web api 2 using taiseer joudeh's excellent blog posts. reasons beyond scope of question, need implement functionality enables admin users log in other users.
after thought came conclusion best way admin request new access token username
of user wants log in as, using current access token. way, know requestor is, , if indeed admin (validated through claims) skip password validation part , issue him new access token based on username
has provided.
now, in owin middleware handling oauth authorization requests, have following code overrides way access tokens issued:
public override async task grantresourceownercredentials(oauthgrantresourceownercredentialscontext context) { using (authrepository _repo = new authrepository()) { bool uservalidated = _repo.validateuser(context.username, context.password); if (!uservalidated) { context.seterror("invalid_grant", "the user name or password incorrect."); return; } } var identity = new claimsidentity(context.options.authenticationtype); identity.addclaim(new claim(claimtypes.name, context.username)); identity.addclaim(new claim("role", "user")); var props = new authenticationproperties(new dictionary<string, string> { { "as:client_id", (context.clientid == null) ? string.empty : context.clientid }, { "username", context.username } }); var ticket = new authenticationticket(identity, props); context.validated(ticket); }
somehow need able see claims of user requesting new access token. oauthgrantresourceownercredentialscontext context
contains necessary information, access token of requestor (should there one). however, unlike in apicontroller
part of web api not have principal
object here, cannot identity
, cast claimsidentity
in order claims me validate requesting user indeed admin.
context.request.user
empty, , seems , information regarding user performing request has not been resolved yet in method. thing accessible me authorization
header containing access token found in context.owincontext
.
is there way me claims of user access token present in request in middleware?
Comments
Post a Comment