Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Per Tenant JWTBearer Options failed with authentication #827

Closed
KarthikEK20 opened this issue May 14, 2024 · 5 comments
Closed

Per Tenant JWTBearer Options failed with authentication #827

KarthikEK20 opened this issue May 14, 2024 · 5 comments
Labels

Comments

@KarthikEK20
Copy link

I'm trying to use the latest version 7.0.1. of the Finbuckle to resolve multitenant and to authorize per tenant basis. Hence, I need to set JWTBearerOptions that is Authority based on tenant-resolved domain URL. I have followed doc, and some older samples,

https://www.finbuckle.com/MultiTenant/Docs/Options
#303

Both have details on how to do it, but if I follow the same, it doesn't work as expected. The problem I'm facing is, that I can see the JWTBearerOption set per tenant by using the "builder.Services.ConfigurePerTenant" line, but still, the authorisation fails because of authority don't have any value.

Here is the example code I followed.

// Configure Services
services.AddHttpContextAccessor();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer();

        services.AddMultiTenant<TenantInfo>()
            .WithStore(ServiceLifetime.Scoped)
            .WithRemoteAuthenticationCallbackStrategy()
            .WithHostStrategy("__tenant__");

        services.ConfigurePerTenant<JwtBearerOptions, TenantInfo>((options, tenantInfo) =>
        {
            options.Authority = "https://mydomain.com/";
            options.Audience = "API"; // API Resource Name
        });

// Configure Middleware

        app.MapControllers();
        app.UseStaticFiles();

        app.UseRouting();

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

        app.UseMultiTenant();

//Note - I tried to position the UseMultiTenant() middleware before Use Authentication line, still no use.

If I tried without "PerTenantOption", the authorisation works. Below is the service part code.

// Configure Services
services.AddHttpContextAccessor();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = "https://mydomain.com/";
options.Audience = "API"; // API Resource Name
}););

        services.AddMultiTenant<TenantInfo>()
            .WithStore(ServiceLifetime.Scoped)
            .WithRemoteAuthenticationCallbackStrategy()
            .WithHostStrategy("__tenant__");

So, changing the JWTBearerOptions using "services.ConfigurePerTenant<JwtBearerOptions, TenantInfo>" is something irrelevant with actual JWTBearerOptions? Could you please explain what I'm missing?

Thanks in advance.

@aswin-ingenuous
Copy link

I also experienced the same issue. It was resolved by specifying the scheme name during ConfigurePerTenant<>().

services.AddAuthentication()
  .AddJwtBearer(); // this line adds `JwtBearerHandler` using `JwtBearerDefaults.AuthenticationScheme` scheme

// important: use the scheme i.e. `JwtBearerDefaults.AuthenticationScheme`
services.ConfigurePerTenant<JwtBearerOptions, TenantInfo>(JwtBearerDefaults.AuthenticationScheme, (options, tenant) =>

@KarthikEK20
Copy link
Author

Thanks, But I tried to add the JwtBearerDefaults.AuthenticationScheme as you suggested, still the authorization fails.

@AndrewTriesToCode
Copy link
Sponsor Contributor

Hi, do you mind posting a link to a repository with a basic project that reproduces the problem?

@KarthikEK20
Copy link
Author

KarthikEK20 commented May 16, 2024

I got it fixed @AndrewTriesToCode . Actually @aswin-ingenuous suggestion works perfect. This is what I missed.

Also, Removing the line .WithRemoteAuthenticationCallbackStrategy() hasn't made any impact.

Somehow, when I tried it yesterday missed to keep the middleware in right orders, So I got the same error. But now I tried this fix with fresh application, and everything works as expected. Thank you guys for the valuable solution.

@aswin-ingenuous
Copy link

aswin-ingenuous commented May 20, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants