question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Role-based authorisation

See original GitHub issue

First of all apologies if this question is really stupid - I’m new to both openiddict and open id connect / oauth.

I’m doing my best to wire up an application through openiddict with the following basic structure:

  1. Authentication Server (.net core web api)
  2. Web Client (.net core web application)
  3. Web Api (.net core web api) - with authorisation

The Web Client is very basic - a static html page that allows: a. The sending of a token request to the Authentication Server, and b. Information retrieval from the web api using the access token received from a. (Jwt)

When I [Authorize] to the controller in my Web Api, the token/client is authorized correctly when both using my Web Client, and through Postman.

However, when I change that to [Authorize(Roles=“admin”)], the request to the Web Api is successful in Postman, but returns a 403 when I make the same request from my Web Client.

I’m probably missing something really simple, but I was just wondering if you could explain to me what is missing / what is needed to allow the authorization against the roles to work when making requests from my Web Client?

My authorization service startup code looks like:

public void ConfigureServices(IServiceCollection services)
{
    // add our db context
    services.AddDbContext<OpenIdDictContext>(options => options.UseSqlServer(Configuration.GetConnectionString("OpenIdDict")));

    // add identity
    // TODO: create custom user manager for custom mhm claims
    services.AddIdentity<OpenIdUser, OpenIdRole>()
        .AddEntityFrameworkStores<OpenIdDictContext>()
        .AddUserManager<OpenIddictUserManager<OpenIdUser>>();

    // add OpenIddict
    // TODO: need to arrange an x509 certificate to use as the signing key
    services.AddOpenIddict<OpenIdUser, OpenIdRole, OpenIdDictContext>()
        .DisableHttpsRequirement()
        .EnableTokenEndpoint("/connect/token")
        .EnableUserinfoEndpoint("/connect/userinfo")
        .AllowPasswordFlow()
        .AllowRefreshTokenFlow()
        .UseJsonWebTokens()
        .AddEphemeralSigningKey();

    services.AddCors();

    // Add framework services.
    services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseCors(builder => builder.WithOrigins("http://localhost:52372"));

    app.UseMvc();

    app.UseOpenIddict();
}

The startup of the web api which is authorizing against the token looks like:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors();
    // Add framework services.
    services.AddMvc();

    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json")
        .Build();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    // use jwt bearer authentication
    app.UseJwtBearerAuthentication(new JwtBearerOptions
    {
        AutomaticAuthenticate = true,
        AutomaticChallenge = true,
        RequireHttpsMetadata = false,
        Audience = "http://localhost:59967/",
        Authority = "http://localhost:59967/",
    });

    app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());

    app.UseMvc();
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
KipRiptidecommented, Aug 22, 2016

Well the weekend has fixed the issue.

In short, I’m an idiot. I had “scopes” instead of “scope” in my web client request. I spent a good few hours last week not being able to spot that typo, but coming in on Monday morning I spotted it straight away.

Sorry for wasting your time, and thanks for responding so quickly to my “issue”!

0reactions
KipRiptidecommented, Aug 19, 2016

Haha, sorry mate, and thanks for responding to my query so promptly.

Would have sent you more code but have left work for the day (in the UK).

Will send you everything when I get back into the office on Monday.

And I’m very sorry to hear about your crystal ball!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Role-based authorization in ASP.NET Core
When an identity is created it may belong to one or more roles. For example, Tracy may belong to the Administrator and User...
Read more >
Role-Based Access Control
Role -based access control (RBAC) refers to the idea of assigning permissions to users based on their role within an organization. It offers...
Read more >
Role-based access control
Role -based access control is a policy-neutral access control mechanism defined around roles and privileges. The components of RBAC such as role-permissions, ...
Read more >
Role-based authorization
Role -based authorization enables customer management of users and their roles independently from Payment Feature Services. Role-based authorization has a ...
Read more >
Authorization Academy - Role-Based Access Control (RBAC)
Role -based authorization, or role-based access control, means grouping permissions into roles, like "User" or "Admin," and assigning those roles to users.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found