eShopOnContainers icon indicating copy to clipboard operation
eShopOnContainers copied to clipboard

Upgrade identityserver4 to 4.1.1

Open GreenShadeZhang opened this issue 3 years ago • 13 comments

Upgrade identityserver4 to 4.1.1, update the database context migration file, after my test, normal use.

GreenShadeZhang avatar Mar 01 '21 15:03 GreenShadeZhang

CLA assistant check
All CLA requirements met.

dnfadmin avatar Mar 01 '21 15:03 dnfadmin

"Sorry, there was an error : invalid_scope" while trying to authorize a swagger project

Identity Logs (here I take basket but it is the same for the other projects):

[23:43:37 INF] Invoking IdentityServer endpoint: IdentityServer4.Endpoints.AuthorizeEndpoint for /connect/authorize

[23:43:37 ERR] Scope basket not found in store.

[23:43:37 ERR] Request validation failed

HighRobot42 avatar Mar 16 '21 23:03 HighRobot42

Hi @GreenShadeZhang , thank you for submitting this PR.

Did a quick test in local and could see following error for the WebSPA and WebMVC. Could you please check at your end ?

WebSPA

image

WebMVC

image

Thank you @HighRobot42 for pointing it out.

sughosneo avatar Mar 17 '21 06:03 sughosneo

Hi @GreenShadeZhang , thank you for submitting this PR.

Did a quick test in local and could see following error for the WebSPA and WebMVC. Could you please check at your end ?

WebSPA

image

WebMVC

image

Thank you @HighRobot42 for pointing it out.

identityserver4 mainly uses apiscope so when I initialized the apiscope invalid_scope error disappeared, invalid_request was because the mvc client RequestPkce default value was true, but the mvc redirect login page did not bring the required parameters, when I chose to set it to false, the request was normal, But I'm not sure if the mvc project matches the authentication service, and I may need to modify the mvc project if necessary.

GreenShadeZhang avatar Mar 17 '21 14:03 GreenShadeZhang

"Sorry, there was an error : invalid_scope" while trying to authorize a swagger project

Identity Logs (here I take basket but it is the same for the other projects):

[23:43:37 INF] Invoking IdentityServer endpoint: IdentityServer4.Endpoints.AuthorizeEndpoint for /connect/authorize

[23:43:37 ERR] Scope basket not found in store.

[23:43:37 ERR] Request validation failed

Thank you @HighRobot42 for pointing it out.

You can use the latest code test

GreenShadeZhang avatar Mar 18 '21 10:03 GreenShadeZhang

I can't get the authorization within the basket swagger ui with your solution. After login, it does not redirect me to the consent html page. Controllers return 401.

HighRobot42 avatar Mar 21 '21 19:03 HighRobot42

I can't get the authorization within the basket swagger ui with your solution. After login, it does not redirect me to the consent html page. Controllers return 401.

Can you provide specific information? For example, which client id you used for the test, or provide me with a link to sample code for testing.

GreenShadeZhang avatar Mar 22 '21 02:03 GreenShadeZhang

I have just added to the swagger clients the property RequireConsent = true in the Config.cs The default value for this property in the class Client has switched from true to false with the recent update. Otherwise the solution remains the same as yours and when I try a basket controller i get "www-authenticate: Bearer error="invalid_token"error_description="The audience 'empty' is invalid"

HighRobot42 avatar Mar 22 '21 21:03 HighRobot42

I finally solved it this way:

In basket client config (Config.cs in Identity solution) I have set the three following properties: RequireConsent = true, IncludeJwtId = false, RequirePkce = false,

Helped by this post from Stackoverflow https://stackoverflow.com/questions/61814688/how-to-add-an-aud-claim-to-access-token

I have updated the basket Startup.cs with the following lines (property TokenValidationParameters)


private void ConfigureAuthService(IServiceCollection services)
        {
            // prevent from mapping "sub" claim to nameidentifier.
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub");

            var identityUrl = Configuration.GetValue<string>("IdentityUrl");

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

            }).AddJwtBearer(options =>
            {
                options.Authority = identityUrl;
                options.RequireHttpsMetadata = false;
                options.Audience = "basket";

                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateAudience = false
                };

            });
        }

Controllers now return 200 for the basket swagger ui. Let me know what you think about my modifications. The above parameters also have to be updated the same way in the other solutions.

HighRobot42 avatar Mar 23 '21 00:03 HighRobot42

I finally solved it this way:

In basket client config (Config.cs in Identity solution) I have set the three following properties: RequireConsent = true, IncludeJwtId = false, RequirePkce = false,

Helped by this post from Stackoverflow https://stackoverflow.com/questions/61814688/how-to-add-an-aud-claim-to-access-token

I have updated the basket Startup.cs with the following lines (property TokenValidationParameters)


private void ConfigureAuthService(IServiceCollection services)
        {
            // prevent from mapping "sub" claim to nameidentifier.
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub");

            var identityUrl = Configuration.GetValue<string>("IdentityUrl");

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

            }).AddJwtBearer(options =>
            {
                options.Authority = identityUrl;
                options.RequireHttpsMetadata = false;
                options.Audience = "basket";

                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateAudience = false
                };

            });
        }

Controllers now return 200 for the basket swagger ui. Let me know what you think about my modifications. The above parameters also have to be updated the same way in the other solutions.

I feel that your configuration can be simplified. The configuration in the demo demo of identityserver4.x is relatively simplified, and you should use the implicit mode to configure swagger. You can refer to the official demo to use.

https://github.com/IdentityServer/IdentityServer4/tree/main/samples/Quickstarts

GreenShadeZhang avatar Mar 23 '21 02:03 GreenShadeZhang

All right but have you tested your own solution? They have changed some default values in the Client class that you have not taken into account. I m not really sure I understand your point btw.

HighRobot42 avatar Mar 23 '21 22:03 HighRobot42

Identityserver3 and identityserver4 are a bit different, so some projects need to adjust the way of authentication use, I adjusted the normal function of webmvc, for different clients also need to adjust the corresponding adjustment, this needs to be tested and modified for the project. I dealt with the main conflict, the overall project should be able to run, I hope that after testing can merge branches, and then further development, to ensure the integrity of the whole. QQ截图20220605164042

GreenShadeZhang avatar Jun 05 '22 08:06 GreenShadeZhang

Can this pr be merged?

GreenShadeZhang avatar Jun 23 '22 06:06 GreenShadeZhang

IdentityServer4 has been upgraded to Duende IdentityServer 6.2 via PR: https://github.com/dotnet-architecture/eShopOnContainers/pull/2012. This PR can be closed now.

erjain avatar Feb 22 '23 07:02 erjain