OAuth token authorization (request has been denied)
<p>I have a WebApi 2 and a MVC Web project in the same solution running on different IIS ports. After recieving my Oauth token using jQuery AJAX I still get a 401 Unauthorized error message when trying to call an authorized Controller method.</p>

<p><a href="https://i.stack.imgur.com/9mOZY.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9mOZY.jpg" alt="enter image description here"></a></p>

<p>Startup:</p>

<pre><code>public void Configuration(IAppBuilder app)
{
    HttpConfiguration httpConfig = new HttpConfiguration();
    ConfigureOAuthTokenGeneration(app);
    ConfigureOAuthTokenConsumption(app);
    ConfigureWebApi(httpConfig);
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    app.UseWebApi(httpConfig);
}
</code></pre>

<p>CustomOAuthProvider:</p>

<pre><code>public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    var userManager = context.OwinContext.GetUserManager&lt;UserManager&gt;();
    User user = await userManager.FindAsync(context.UserName, context.Password);

    // checks with context.SetError() results.

    ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
    oAuthIdentity.AddClaim(new Claim(ClaimTypes.Role, "User"));

    var ticket = new AuthenticationTicket(oAuthIdentity, null);
    context.Validated(ticket);
}
</code></pre>

<p>Thinks I've tried from <a href="https://stackoverflow.com/questions/26458785/i-get-authorization-has-been-denied-for-this-request-error-message-when-using">I get &quot;Authorization has been denied for this request.&quot; error message when using OWIN oAuth middleware (with separate Auth and Resource Server)</a>:</p>

<ol>
<li>Updating all the Owin packages to latest version (Web project does not use any Owin functionality so it is not installed here).</li>
<li>Api and web are different projects but on same machine, so same machinekey.</li>
<li>OAuth Token configuration comes before the WebApi configuration in Startup.cs.</li>
<li>Claims are made: oAuthIdentity consist out of a role and an admin claim (<a href="http://schemas.microsoft.com/ws/2008/06/identity/claims/role" rel="nofollow noreferrer">http://schemas.microsoft.com/ws/2008/06/identity/claims/role</a>: User)</li>
</ol>

<p>Everything else works as expected (web api, cors, token generation,...), what am I doing wrong? (There is a lot of code involved, so let me know if I need to place an other piece of code from my projects.</p>

<p><strong>EDIT:</strong></p>

<p>Ajax call (Solution by jumuro):</p>

<pre><code>var token = sessionStorage.getItem(tokenKey); // Same as the generated login token
$.ajax({
    type: 'POST',
     // Don't forget the 'Bearer '!
    beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', 'Bearer ' + token) },
    url: 'http://localhost:81/api/auth/test', // Authorized method
    contentType: 'application/json; charset=utf-8'
}).done(function (data) {
    //
});
</code></pre>