Code Monkey home page Code Monkey logo

Comments (7)

Shane32 avatar Shane32 commented on August 18, 2024

Have you verified that the client is passing the JWT bearer token in the authorize header? Can you post a copy of the entire Authorize header including the JWT token here?

from authorization.

HnatiukSerhiy avatar HnatiukSerhiy commented on August 18, 2024

image
image
Is it enough? Because I am not sure if it possible to review the whole header in altair.

from authorization.

Shane32 avatar Shane32 commented on August 18, 2024

I have JWT authentication working in a few of my installations but I still don’t immediately see anything wrong with your setup.

I suggest you create a git repository with your progress so far and I can download it and take a look.

from authorization.

HnatiukSerhiy avatar HnatiukSerhiy commented on August 18, 2024

https://github.com/HnatiukSerhiy/GraphQLAuthorisationTest

from authorization.

Shane32 avatar Shane32 commented on August 18, 2024

Reference the GraphQL.Server.Authorization.AspNetCore NuGet package and not the GraphQL.Authorization package.

Then change your startup like this:

using GraphQL;
using GraphQL.MicrosoftDI;
using GraphQL.SystemTextJson;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using Test.Api;
using Test.Repository;
using GraphQL.Types;
using GraphQL.Server;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<IEmployeeRepository, EmployeeRepository>();

builder.Services.AddControllersWithViews();

builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateAudience = true,
        ValidateIssuer = true,
        ValidateIssuerSigningKey = true,
        ValidAudience = "audience",
        ValidIssuer = "issuer",
        RequireSignedTokens = false,
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("secretsecretsecret"))
    };

    options.RequireHttpsMetadata = false;
    options.SaveToken = true;
});

builder.Services.AddAuthorization(policyBuilder =>
{
    policyBuilder.AddPolicy("UserPolicy", p => p.RequireClaim(System.Security.Claims.ClaimTypes.Role, "User"));
});

builder.Services.AddGraphQL(b => b
                .AddHttpMiddleware<ISchema>()
                .AddGraphQLAuthorization()
                .AddSystemTextJson()
                .AddErrorInfoProvider(opt => opt.ExposeExceptionStackTrace = true)
                .AddSchema<AppSchema>()
                .AddGraphTypes(typeof(AppSchema).Assembly));

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseCors("AllowAll");

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseGraphQL<ISchema>();

app.UseGraphQLAltair();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

You can also delete the GraphQLUserContext class. (Or you can continue to use it for other purposes; but I removed the reference from the startup.)

Notice that p.RequireClaim(System.Security.Claims.ClaimTypes.Role, "User") was changed from p.RequireClaim("role", "User").

from authorization.

Shane32 avatar Shane32 commented on August 18, 2024

I'm sure there's a way to use the GraphQL.Authorization package, but I'm not familiar with that package; sorry. The GraphQL.Server.Authorization.AspNetCore package is part of GraphQL.Server and designed for use with ASP.NET Core

from authorization.

HnatiukSerhiy avatar HnatiukSerhiy commented on August 18, 2024

No need to apologize, you helped me a lot. Thank you!

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.