Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Owin Bearer Token Not Working for WebApi

I have gone through loads of documentation on this, My google search shows that I've visited all the links on the first page

Problem Token Generation works fine. I configured it with a custom provider as such:

    public void ConfigureOAuth(IAppBuilder app)
    {
        var usermanager = NinjectContainer.Resolve<UserManager>(); 
        app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new AppOAuthProvider(usermanager)
        });
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
    }

However when I call a protected URL and pass the bearer token, I always get: Token Request

How do I diagnose or fix the problem. If possible, how can I do the token validation myself

UPDATE Here is my AppOAuthProvider. Both Methods are called when I'm trying to mint a token but not when I'm trying to access a protected resource

public class AppOAuthProvider : OAuthAuthorizationServerProvider
{
    private UserManager _user;
    public AppOAuthProvider(UserManager user)
    {
        _user = user;
    }
    public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        context.Validated();
    }

    public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

        //Get User Information
        var getUser = _user.FindUser(context.UserName);
        if (getUser.Status == StatusCode.Failed)
        {
            context.SetError("invalid_grant", "The user name or password is incorrect.");
            return Task.FromResult<object>(null);
        }

        var user = getUser.Result;

        //Get Roles for User
        var getRoles = _user.GetRoles(user.UserID);
        if (getRoles.Status == StatusCode.Failed)
        {
            context.SetError("invalid_grant", "Could not determine Roles for the Specified User");
        }

        var roles = getRoles.Result;

        var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        identity.AddClaim(new Claim("UserID", user.UserID.ToString()));
        identity.AddClaim(new Claim("UserName", user.UserName));

        foreach (var role in roles)
        {
            identity.AddClaim(new Claim(ClaimTypes.Role, role));
        }

        context.Validated(identity);

        return Task.FromResult<object>(null);
    }
}

UPDATE 2: Here is my Account Controller

[RoutePrefix("api/auth/account")]
public class AccountController : ApiController
{
    private UserManager _user;
    public AccountController(UserManager user)
    {
        _user = user;
    }

    [Authorize]
    [HttpGet]
    [Route("secret")]
    public IHttpActionResult Secret()
    {
        return Ok("Yay! Achievement Unlocked");
    }
}

UPDATE 3: Here is my Startup.cs

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseNinjectMiddleware(NinjectContainer.CreateKernel);
        app.UseNinjectWebApi(GlobalConfiguration.Configuration);
        GlobalConfiguration.Configure(WebApiConfig.Register);
        ConfigureOAuth(app);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(GlobalConfiguration.Configuration);
        app.UseWelcomePage();
    }
}
like image 312
Ody Avatar asked Aug 25 '14 10:08

Ody


People also ask

How do I pass a bearer token in API?

Bearer tokens enable requests to authenticate using an access key, such as a JSON Web Token (JWT). The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type dropdown list. In the Token field, enter your API key value.

How bearer token works in Web API?

You use the bearer token to get a new Access token. To get an access token you send the Authentication server this bearer token along with your client id. This way the server knows that the application using the bearer token is the same application that the bearer token was created for.


1 Answers

You must configure the OAuth authorization server and OAuth bearer authentication before call UseWebApi on IAppBuilder. The following is from my program.

    public void Configuration(IAppBuilder app)
    {
        app.UseFileServer(new FileServerOptions()
        {
            RequestPath = PathString.Empty,
            FileSystem = new PhysicalFileSystem(@".\files")
        });

        // set the default page
        app.UseWelcomePage(@"/index.html");

        ConfigureAuth(app);

        HttpConfiguration config = new HttpConfiguration();

        config.Routes.MapHttpRoute
        (
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional } 
        );

        config.Formatters.Clear();
        config.Formatters.Add(new JsonMediaTypeFormatter());
        config.Formatters.JsonFormatter.SerializerSettings =
        new JsonSerializerSettings
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        };

        app.UseCors(CorsOptions.AllowAll);
        app.UseWebApi(config);


    }

    public void ConfigureAuth(IAppBuilder app)
    {
        OAuthAuthorizationServerOptions oAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new YourApplicationOAuthProvider()
        };

        app.UseOAuthAuthorizationServer(oAuthServerOptions);
        app.UseOAuthBearerAuthentication
        (
            new OAuthBearerAuthenticationOptions 
            {
                Provider = new OAuthBearerAuthenticationProvider()
            }
        );
    }
like image 106
Yang Zhang Avatar answered Oct 03 '22 13:10

Yang Zhang