My WebAPI 2 application has a custom authorization filter that checks the access token. If a token is present and the API has an attribute, then I check to see if there is a user that maps to this token.
Due to the nature of the API, most methods are run in the context of a particular user (that is, the "POST api / profile" to update the user profile). To do this, I need the information about the target user that I get from the access token.
[Current implementation, happening inside an attribute of type AuthorizeAttribute]
if( myDBContext.MyUsers.Count( x => x.TheAccessToken == clientProvidedToken ) ){ IPrincipal principal = new GenericPrincipal( new GenericIdentity( myAccessToken ), new string[] { "myRole" } ); Thread.CurrentPrincipal = principal; HttpContext.Current.User = principal; return true; }
This works fine, and I can use the access token to perform a second search in the method. But since Iβm already doing auth searches, I donβt want to spend another DB call .
[What I would like to do (but obviously does not work)]
MyUser user = myDBContext.MyUsers.FirstOrDefault( x => x.TheAccessToken == clientProvidedToken ); if( user != null ){
authentication c # authorization asp.net-web-api
Shanec
source share