Retrieve user information from Web API OAuth Bearer authentication

Viewed 5114

I have a working Web API using Token validation but I want to retrieve the user who sent that token.

Request.GetOwinContext().Authentication.User.Identity.Name; // returns null

How can I achieve that?

1 Answers

I use owin and get the current user this way

ControllerContext.RequestContext.Principal.Identity.Name;

to check if it is authenticated

ControllerContext.RequestContext.Principal.Identity.IsAuthenticated

Be sure the username is linked to the token this way (it is on my

SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider

class)

 ClaimsIdentity oAuthIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
                oAuthIdentity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
Related