I've recently joined a project team and have been tasked with setting up User Roles for the backend of the project. It uses ASP.NET C# WebApi with Owin. My issue is, when I assign an attribute to the Controller Method like this:
[Authorize(Roles = "Admin")]
The response is always Authorization denied for this request. However if I simply use:
[Authorize]
It works. Note that I am logging in with a User that has been assigned the Role of Admin.
I've noticed that this question is similar to: Authorization roles WebAPI oauth owin
However, it seems their code in startup.cs is different somehow, or else I'm struggling to follow the answer correctly.
The code in the startup.cs that I have to work with is:
public void Configuration(IAppBuilder app)
{
// configure OAuth
ConfigureOAuth(app);
// configure Mvc
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
}
public void ConfigureOAuth(IAppBuilder app)
{
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
Is there something else I need to add in here to allow for Roles or should it be somewhere else in the code. I'm not completely familiar with ASP.NET C# MVC or WebApi, any help is severely appreciated.