I have a minimal setup of an auth provider that sets claim identifiers
public class SimpleAuthorizationProvider : OAuthAuthorizationServerProvider { public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) { context.Validated(); } public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { if (context.UserName != context.Password) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim("sub", context.UserName)); identity.AddClaim(new Claim("role", "user")); context.Validated(identity); } }
I am trying to access hello-world-api which gives an unauthorized access error.
public class HelloWorldApiController : ApiController { [HttpGet] [Route("api/hello")]
But I get 401 / unauthorized access for the above API. I am returning the carrier token back to the web-api and I am also transferring it to the server as Bearer ABCD**** . I see that the authorization header is set when debugging in Visual Studio.
If I debug AuthorizeAttribute , I get user.Identity.IsAuthenticated as false , which actually causes the problem. But given that I see a set of authorization headers, and I asked the details of the claims in OAuthProvider , why doesn't it AuthorizeAttribute read this information?
Note. This is a web API project, so there are no references to the AuthorizeAttribute MVC attribute.
Here is the installation of OWIN:
public static class WebApiConfig { public static HttpConfiguration Register() { var config = new HttpConfiguration(); config.MapHttpAttributeRoutes();
source share