abpframework / abp

Open Source Web Application Framework for ASP.NET Core. Offers an opinionated architecture to build enterprise software solutions with best practices on top of the .NET and the ASP.NET Core platforms. Provides the fundamental infrastructure, production-ready startup templates, application modules, UI themes, tooling, guides and documentation.
https://abp.io
GNU Lesser General Public License v3.0
12.31k stars 3.32k forks source link

Issue with Token Authentication and User Login Information after Upgrading to Abp Framework Version 8 #18895

Closed abdullahshaqaliah closed 2 months ago

abdullahshaqaliah commented 3 months ago

Dear Support Team,

I trust this message finds you well. I am reaching out to you again regarding the issue I reported earlier after upgrading from Abp Framework version 7 to version 8. To provide more clarity and assist in the investigation, I have attached a screenshot that illustrates the problem.

In the attached image, you'll notice that when sending a token to the API, only the user's name and some additional data are being transmitted. However, there is no indication of the login status or user ID, which was a part of the expected behavior before the upgrade.

I believe that this visual representation will help in better understanding the issue at hand. Please review the attached screenshot and let me know if there are any specific details or logs you require for a more in-depth analysis.

Thank you for your attention to this matter. I appreciate your prompt assistance in resolving this critical issue.

WhatsApp Image 2024-01-30 at 12 35 31 PM

abdullahshaqaliah commented 3 months ago

@maliming ???

maliming commented 3 months ago

hi

Steps needed to reproduce the problem.

abdullahshaqaliah commented 3 months ago

@maliming Did you see the picture ??? why other information like Id and roles not sent with the access token with Abp 7 it's working after just upgrading to 8 if face the problem Please see the picture

maliming commented 3 months ago

Hi

I can't figure out the problem by a picture.

abdullahshaqaliah commented 3 months ago

I already login by Swager then I make request by API

abdullahshaqaliah commented 3 months ago

@maliming See the picture why the current user is not login and not hold IsAuthenticated and id and user roles?? WhatsApp Image 2024-01-30 at 2 47 34 PM

abdullahshaqaliah commented 3 months ago

I used this PackageReferences with microservices `

    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.1.0" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.3.0" />
    <PackageReference Include="TenTime.AdministrationService.EntityFrameworkCore" Version="4.0.0" />
    <PackageReference Include="TenTime.Redis.OM.Repository" Version="3.0.0" />
    <PackageReference Include="Volo.Abp.EventBus.RabbitMQ" Version="8.0.2" />
    <PackageReference Include="Volo.Abp.BackgroundJobs.RabbitMQ" Version="8.0.2" />
    <PackageReference Include="TenTime.Shared.Hosting.Settings" Version="6.0.0" />
    <PackageReference Include="Volo.Abp.Http.Client.IdentityModel.Web" Version="8.0.2" />
    <PackageReference Include="Volo.Abp.Identity.AspNetCore" Version="8.0.2" />
    <PackageReference Include="Volo.Abp.DistributedLocking" Version="8.0.2" />  
    <PackageReference Include="DistributedLock.Redis" Version="1.0.2" />
    <PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="8.0.0" />
    <PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.0" />
</ItemGroup>`
abdullahshaqaliah commented 3 months ago

If you see the user token already have user information image

abdullahshaqaliah commented 3 months ago

@maliming See the claims already hold all user information the problem with ICurrentUser is not hold all user information

WhatsApp Image 2024-01-30 at 3 08 17 PM

abdullahshaqaliah commented 3 months ago

@maliming ???

maliming commented 3 months ago

I don't know your code. But you can try:

AbpClaimTypes.UserId = JwtClaimTypes.Subject;
AbpClaimTypes.Email = JwtClaimTypes.Email;

You can share your jwtbearer authentication code.

abdullahshaqaliah commented 3 months ago

[DependsOn( typeof(AccountServiceHttpApiModule), typeof(TenTimeSharedHostingSettingsModule), typeof(AccountServiceApplicationModule), typeof(AdministrationServiceEntityFrameworkCoreModule), typeof(AccountServiceEntityFrameworkCoreModule), typeof(AbpIdentityAspNetCoreModule), typeof(AbpBackgroundJobsRabbitMqModule), typeof(AbpEventBusRabbitMqModule), typeof(AbpDistributedLockingModule), typeof(AbpHttpClientIdentityModelWebModule), typeof(TenTimeRedisOMRepositoryModule), typeof(AbpSecurityModule), typeof(TenTimeSecurityModule))] `

private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
{

    context.Services.AddAuthentication("Bearer").AddJwtBearer(delegate (JwtBearerOptions options)
    {
        options.Authority = configuration["AuthServer:Authority"];
        options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]);
        options.Audience = configuration["AuthServer:ApiName"];
    });
}

`

maliming commented 3 months ago

Does this work?

AbpClaimTypes.UserId = ClaimTypes.NameIdentifier;
AbpClaimTypes.Email = ClaimTypes.Email;
abdullahshaqaliah commented 3 months ago

@maliming its working Where is the problem there need any module to load??

maliming commented 3 months ago

I'm not sure unless I can see the complete code.

abdullahshaqaliah commented 3 months ago

@maliming What is the module name responsible for assigning the AbpClaimTypes??

abdullahshaqaliah commented 3 months ago

@maliming The problem happen just when upgrade from 7 to 8 before its working we are not change the our code

maliming commented 3 months ago

I'm not sure unless I can see the complete code.

agustinsilvano commented 2 months ago

@maliming @abdullahshaqaliah Not sure if we are using the same setup but I'm using the old Auth Server (Identity Server 4, we are planning to migrate to OpenIdDict shortly). I made the upgrade from v7 (where the application was working fine) to v8 and faced the same issue that Abdullah.

Seems like the there is a miss match on the naming of the claims that abp uses to establish the current user and roles. I also found that the application-configuration endpoint was missing the permissions assigned to the logged user because it was not able to resolve properly the roles stored on the JWT token.

What I did?

I had to add the following mapping to the module definition of my web api projects.

AbpClaimTypes.UserId = ClaimTypes.NameIdentifier;
AbpClaimTypes.Email = ClaimTypes.Email;
AbpClaimTypes.Role = ClaimTypes.Role;
AbpClaimTypes.UserName = ClaimTypes.Name;

By adding that the role key called "http://schemas.microsoft.com/ws/2008/06/identity/claims/role" instead of the regular role, it does the trick. I tried to create a JWT token using OpenIdDict but I found that you are using the role key instead of the url-wise key.

If I decode my token using jwt.io I see image

I want to know if that is expected, worried about having a clunky setup.

By having the default values it didnt work, could be possible that something overrode the value of these AbpClaimTypes class in the v8?

agustinsilvano commented 2 months ago

In addition to my last comment, digging into the source code I found this AbpClaimsMapOptions class that is being used in AbpClaimsMapMiddleware class.

So seems like the old sub and role claims must be mapped to the appropiate AbpClaimTypes, isn't it?

Don't fully understand why we need to set the values of the static properties of the AbpClaimTypes class.

maliming commented 2 months ago

hi @agustinsilvano

Can you share a simple project?

agustinsilvano commented 2 months ago

@maliming sadly I cant share the code given that it has sensitive information.

I'll try to do a sample with a blank solution.

Actually, let me ask you something, should the AbpClaimsMapMiddleware be involved on this process of establishing the current user and all that info?

I tried to override it but it was never called on the request flow.

agustinsilvano commented 2 months ago

@maliming I migrated a project that I had in v7.3 (with OpenIdDict) and the upgrade worked fine. So, seems like is related to the Identity Server setup.

Let me ask you, what's the class that does that mapping? image

For example, If I decode the JWT token the user id "sub" property is transformed into "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier".

maliming commented 2 months ago

hi

So, seems like is related to the Identity Server setup.

It depends on your project, that's why I've asked many times for a reproduction project

agustinsilvano commented 2 months ago

@maliming here you have a sample vanilla project with the same issue.

There is a TestAppService that checks for the roles of the current user, I was able to reproduce the same issue with that project. image

I started from v4.3.3 and upgraded to v8.0.4.

Hopefully it helps to find the root cause.

maliming commented 2 months ago

hi

Thanks. I will check your project.

maliming commented 2 months ago

hi

There are two solutions in your HttpApi.Host project:

1 UpdateAbpClaimTypes

public override void PreConfigureServices(ServiceConfigurationContext context)
{
    context.Services.PreConfigure<AbpIdentityServerBuilderOptions>(options =>
    {
        options.UpdateAbpClaimTypes = false;
    });
}

2 MapInboundClaims.

context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        options.Authority = configuration["AuthServer:Authority"];
        options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]);
        options.Audience = "BookStore2";
        options.MapInboundClaims = false;
    });
agustinsilvano commented 2 months ago

@maliming thanks for the quick response!

It worked like a charm! 👏🏽👏🏽👏🏽

antonGritsenko commented 1 week ago

@maliming which one is better? What side effect of setting UpdateAbpClaimTypes to false? PS IMO this information must be added to the migration guide, this case 100% reproducible when upgrading from 7 to 8 with old IdentityServer.

maliming commented 1 week ago

hi @antonGritsenko

Both are fine. It depends on your actual project.

I have posted an article about claim type. https://community.abp.io/posts/how-claim-type-works-in-asp-net-core-and-abp-framework-km5dw6g1