title: Retrieving User Identity from OWIN OAuth Authentication Server
body: <p>I have two MVC web applications in my project. The first acts as an OAuth authentication server and deals solely with log ins and user authentication, while the second is a standard MVC application with some public and some protected controllers.</p>
  
  <p>I am using a custom OWIN <code>OAuthAuthorizationServerProvider</code> in my Auth Server and DotNetOpenAuth in my client application.</p>
  
  <p>Currently, I am able to redirect my client application to my auth server's login pages, log in and redirect back with an access token in the query string, which I'm happy with for the moment. However, I need to get access to <code>User.Identity</code> within my client application to do things like get the User Id, their claims, whether they are authenticated etc.</p>
  
  <p>I noticed that the Owin <code>AuthenticationTokenProvider</code> can receive an access token and deserialise it into <code>AuthenticationTokenReceiveContext</code> which has the an identity object within it, however I cannot find any information about how to make this occur from my client application.</p>
  
  <p>How do I exchange an access token for a user's identity from an application external to the OAuth Server itself?</p>
  
  <p>I've been using this walkthrough: <a href="http://www.asp.net/aspnet/overview/owin-and-katana/owin-oauth-20-authorization-server" rel="nofollow">http://www.asp.net/aspnet/overview/owin-and-katana/owin-oauth-20-authorization-server</a> but it uses WebAPI as the resource server and doesn't really go into detail about how to get the user.</p>
  
  <p>My <strong>Authentication Server</strong> <code>Startup.Auth.cs</code>:</p>
  
  <pre><code>public void ConfigureAuth(IAppBuilder app)
  {
       // authentication provider setup, cookies etc...
  
       var oAuthOptions = new OAuthAuthorizationServerOptions
                          {
                              Provider = new ApplicationOAuthProvider(),
                              AuthorizationCodeProvider = new AuthenticationCodeProvider(),
                              RefreshTokenProvider = new RefreshCodeProvider(),
                              AuthorizeEndpointPath = new PathString("/Account/Authorize"),
                              TokenEndpointPath = new PathString("/token"),
                              AccessTokenExpireTimeSpan = TimeSpan.FromDays(30)
                          };
  
       app.UseOAuthAuthorizationServer(oAuthOptions);
  }
  </code></pre>
  
  <p><strong>AuthenticationCodeProvider.cs</strong> (saving the tokens like this is temporary)</p>
  
  <pre><code>public class AuthenticationCodeProvider : AuthenticationTokenProvider
  {
      private readonly ConcurrentDictionary&lt;string, string&gt; _authenticationCodes =
          new ConcurrentDictionary&lt;string, string&gt;(StringComparer.Ordinal);
  
      public AuthenticationCodeProvider()
      {
          OnCreate += CreateAuthenticationCode;
          OnReceive += ReceiveAuthenticationCode;
      }
  
      private void CreateAuthenticationCode(AuthenticationTokenCreateContext context)
      {
          context.SetToken(Guid.NewGuid().ToString("n") + Guid.NewGuid().ToString("n"));
          _authenticationCodes[context.Token] = context.SerializeTicket();
      }
  
      private void ReceiveAuthenticationCode(AuthenticationTokenReceiveContext context)
      {
          string value;
          if (_authenticationCodes.TryRemove(context.Token, out value))
          {
              context.DeserializeTicket(value);
          }
      }
  }
  </code></pre>