Pergunta

I am using the individual user accounts part of .NET 4.5 to authenticate and authorise users to a RESTful API (http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api)

However, I need to work out how to access the user ID from the bearer authorisation token, so I can return the data for the correct user.

How do I determine the correct user ID in a stateless API?

Foi útil?

Solução

Once WebAPI has accepted the Bearer token and authenticated/authorised the user, you can just use User.Identity to get the user name from within the API Controller code:

e.g.

// GET api/values
        public IEnumerable<string> Get()
        {
           var userName = User.Identity.Name;
           //and so on...
        }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top