Code Monkey home page Code Monkey logo

Comments (7)

kmanev073 avatar kmanev073 commented on July 18, 2024 1

It worked! I had missed: app.UseAuthentication(); and I was passing the Authorization header wrongly. Thank you!

from authorization.

BKB503 avatar BKB503 commented on July 18, 2024 1

@kmanev073 I'm getting same thing, I have added app.UseAuthentication(); didn't work, When I add
var result1 = await httpContext.AuthenticateAsync("Bearer"); in Validation rule then setting the prinicpal to current http context. then claims got populated

I'm not sure what is the best solution and I need it part of ValidationRule or some where global in The GraphQL middlware any help really appreciated

from authorization.

joemcbride avatar joemcbride commented on July 18, 2024
{ "roles", JsonConvert.SerializeObject(user.GetRolesByCompanyId(companyId)) }

My understanding is that would create a roles claim with multiple values, assuming that serializes to an array. The example uses a singular role claim.

Did you try matching the name of the claim to your roles? At a minimum, the name of the claims have to match.

from authorization.

kmanev073 avatar kmanev073 commented on July 18, 2024
{ "roles", JsonConvert.SerializeObject(user.GetRolesByCompanyId(companyId)) }

My understanding is that would create a roles claim with multiple values, assuming that serializes to an array. The example uses a singular role claim.

Did you try matching the name of the claim to your roles? At a minimum, the name of the claims have to match.

Yeah this just creates an array of strings (the role names) and places it inside the token. I should be able access my token claims with context.UserContext.Claims from inside a mutation for example, however my roles aren't there.

from authorization.

joemcbride avatar joemcbride commented on July 18, 2024

Is it populated at all? Is any of the information there?

from authorization.

kmanev073 avatar kmanev073 commented on July 18, 2024

Is it populated at all? Is any of the information there?

Here is a screenshot of my context: https://i.imgur.com/lp6Noch.png
And here's how my JWT looks:

{
  "sub": "3ced78d7-f611-4099-95e0-59e49279cf92",
  "iss": "System",
  "iat": 1542147174,
  "exp": 1542190374,
  "company": "8c7a1275-ba67-47a2-ad8f-d452416b98e9",
  "roles": "[\"Administrator\"]"
}

And I this is my policy:
authSettings.AddPolicy("AdministratorPolicy", _ => _.RequireClaim("roles", "Administrator"));

from authorization.

joemcbride avatar joemcbride commented on July 18, 2024

If you look at that screenshot the Identity.IsAuthenticated is false. That means the ClaimsPrincipal is not getting populated. How have you configured your JwtBearer authentication? Is it set to be enabled by default?

You're passing that in a http Authorization header? You should see the ASP.NET Core JwtBearer middleware handling the request in logs.

Authorization Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IkY0NTI2Qj...
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  .AddJwtBearer(cfg =>
  {
    cfg.TokenValidationParameters = new TokenValidationParameters()
    {
      ValidateIssuer = true,
      ValidIssuer = _config["Security:Tokens:Issuer"],
      ValidateAudience = true,
      ValidAudience = _config["Security:Tokens:Audience"],
      ValidateIssuerSigningKey = true,
      IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Security:Tokens:Key"])),
 
    };
  });

from authorization.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.