nbarbettini / SimpleTokenProvider

Sample middleware component for ASP.NET that generates JWT access tokens
Apache License 2.0
288 stars 90 forks source link

How to access the user in a Controller? #15

Closed raupes closed 7 years ago

raupes commented 7 years ago

Thanks for your work. There is something I just can't figure out. I hope you will help me here. I use your provider on a core web api project. Acting with javascript on the other hand. Therefore I use jwt Bearer Authentication. ` app.UseMiddleware(Options.Create(options));

        app.UseJwtBearerAuthentication(new JwtBearerOptions {
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            TokenValidationParameters = tokenValidationParameters, ...

` All works quite fine. The user gets a token, i can validate if the token is valid. The authorize attribute at the controller works nice. But how can I now access the user in the controller now? How do I know there to which user the current request belongs too? HttpContext.User doesn't give me anyth. Not the Claims-Identity I returned at the GetIdentity and not any claim... Where to set it or where to get it?!

So what I need is a possibility to access the username, claim data, the identity etc. at the controller class.

Please help thank you R

Vaylandt commented 7 years ago

change this^ image

change this: image

ang result theis:

image

nbarbettini commented 7 years ago

Hey @raupes - not sure if you already figured this out, but the UseJwtBearerAuthentication middleware will automatically convert JWT claims into ClaimsIdentity claims for you. You shouldn't have to change any of the code in the example to see this in action.

I added a commit that demonstrates this here: https://github.com/nbarbettini/SimpleTokenProvider/commit/d74333065f8a33e7e58d1f05fcd21abe96766fb9