title: How to get oauth Authorization code in asp.net
body: <p>I am busy implementing an <code>oauth</code> Authorization Server on my <code>ASP.NET</code> WEB API.</p>
  
  <p>I have setup the API to support authorization server:</p>
  
  <p>Startup.Auth.cs</p>
  
  <pre><code>public void ConfigureAuth(IAppBuilder app)
      {
          app.CreatePerOwinContext(IdentityDbContext.Create);           
          app.CreatePerOwinContext&lt;UserManager&gt;(UserManager.Create);
  
          app.UseCookieAuthentication(new CookieAuthenticationOptions());            
          app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
  
          // Configure the application for OAuth based flow
          PublicClientId = "self";
  
          OAuthOptions = new OAuthAuthorizationServerOptions
          {  
              AllowInsecureHttp = true,
              ApplicationCanDisplayErrors = true,
              TokenEndpointPath = new PathString("/Token"),
              Provider = new SimpleAuthorizationServerProvider(),
              RefreshTokenProvider = new SimpleRefreshTokenProvider(),
              AccessTokenExpireTimeSpan = TimeSpan.FromHours(24),
              AuthorizationCodeProvider = new SimpleAuthorizationCodeProvider()
          };
  
          app.UseOAuthBearerTokens(OAuthOptions);
          app.UseOAuthAuthorizationServer(OAuthOptions);
      }
  </code></pre>
  
  <p>I have then implemented my <code>SimpleAuthorizationCodeProvider()</code>:</p>
  
  <pre><code>public class SimpleAuthorizationCodeProvider : IAuthenticationTokenProvider
  {            
  
      public async Task CreateAsync(AuthenticationTokenCreateContext context)
      {
          context.Ticket.Properties.Dictionary.TryGetValue("as:client_id", out string clientId);
  
          //Build holder object
          var holder = new AuthorizationCodeHolder(0, clientId, context.SerializeTicket());
  
          var protectedTicket = AesSymmetricalEncryption.EncryptData(Key, JsonConvert.SerializeObject(holder));
          //Set the Token for the request
          context.SetToken(protectedTicket);
      }
  
  
      public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
      {
          //Decrypt token
          var token = AesSymmetricalEncryption.DecyptData(Key, context.Token);
          //Convert back to object
          var ticket = JsonConvert.DeserializeObject&lt;AuthorizationCodeHolder&gt;(token);            
          //
          context.DeserializeTicket(ticket.Ticket);
      }
  </code></pre>
  
  <p>However, now my issue is, I do not know how to generate this authorization code?</p>
  
  <p>I have a section on a separate MVC site, where a user logs in. After they have done that, I need to return a response with a parameter <code>code={auth code}</code>. How do I go about generating this authorization code?</p>
  
  <p>Do I need to login to the api, and then call a specific endpoint? or can I just use the owin authentication in the MVC site to generate it?</p>
  
  <p>I have tested the api, and I can login using an authentication code, but I just do not know how to generate a valid one?</p>