ASP.NET Core 5 with IdentityServer4: in loop after the login

Enrico Rossini 196 Reputation points
2021-02-03T09:16:21.25+00:00

I have my new project with ASP.NET Core 5 and ASP.NET Core Identity. I want to add AddOpenIdConnect with IdentityServer4. I have other projects already in place that they are working fine and up and running. Then, I copied the code and paste in my new project.

services.AddSession(options =>  
{  
    options.Cookie.Name = ".puresourcecode.session";  
    options.IdleTimeout = TimeSpan.FromHours(12);  
});  
  
var idsrv = Configuration.GetSection("IdentityAuthentication").Get<IdentityServerSettings>();  
if (idsrv.UseIdentityServer)  
{  
    services.AddAuthentication(options =>  
    {  
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;  
        options.DefaultChallengeScheme = "oidc";  
    })  
    .AddCookie(options =>  
    {  
        options.ExpireTimeSpan = TimeSpan.FromMinutes(30);  
        options.Cookie.Name = "puresourcecode.cookie";  
    })  
    .AddOpenIdConnect("oidc", options =>  
    {  
        options.Authority = idsrv.IdentityServerUrl;  
        options.ClientId = idsrv.ClientId;  
        options.ClientSecret = idsrv.ClientSecret;  
  
#if DEBUG  
        options.RequireHttpsMetadata = false;  
#else  
        options.RequireHttpsMetadata = true;  
#endif  
  
        options.ResponseType = "code";  
  
        options.Scope.Clear();  
        options.Scope.Add("openid");  
        options.Scope.Add("profile");  
        options.Scope.Add("email");  
        options.Scope.Add("roles");  
        options.Scope.Add("offline_access");  
  
        options.ClaimActions.MapJsonKey("role", "role", "role");  
  
        options.GetClaimsFromUserInfoEndpoint = true;  
        options.SaveTokens = true;  
  
        options.SignedOutRedirectUri = "/";  
  
        options.TokenValidationParameters = new TokenValidationParameters  
        {  
            NameClaimType = JwtClaimTypes.Name,  
            RoleClaimType = JwtClaimTypes.Role,  
        };  
    });  
}  

I added the new package Microsoft.AspNetCore.Authentication.OpenIdConnect. I configured my IdentityServer to redirect the user after the login to /signin-oidc and the grant types is authorization_code. As I said I have lot of other projects that are working fine.

When I run the new application, I redirect to the IdentityServer. I login but the the browser is in loop because it is opening again and again the IdentityServer page for authentication (I don't need to authenticate again because I have already authenticated myself).

https://identityserver.azurewebsites.net/connect/authorize?client_id=GenericDashboard&redirect_uri=https%3A%2F%2Flocalhost%3A44396%2Fsignin-oidc&response_type=code&scope=openid%20profile%20email%20roles%20offline_access&code_challenge=H9RoyXfqahr-f4DtyDgdNkh_8vGUST_QhgjU2VYJXdQ&code_challenge_method=S256&response_mode=form_post&nonce=637478860376726598.ZmRhOTRiNmEtZDllMC00NjY5LThmYzItNjc0Yzk3YjllZGExMjQwOGE3MTctYmJmMS00NmNkLWExNzgtZjczYzE3ZmNhOTgw&state=CfDJ8LL4MQ6yNcxBhaWzp8huStleQClOdf0oyg9kbVOBOKlghZFxCaIifcPydVsUC4cpcN1cGZ9tlFQ38G4zO7jl_boSmcwyUhEg3GDOQ6xBOGYPD-pbFrm3gPWjUp2jZx2Ex0lYMH6y9lt9V4ZZ4kSbIPb08kaN-6i429yXW3oOQ3Pc8e1GtA7RprRWoinyywFp-nt2kGu9K4K17NIrtUmCQ3SUXxCoI3AncQIJoi4wLrndzchVzkc7W3lT39ZbmP8-e7_s4rYrogGR_gQ2N_I4OqYfVnVzDt1drJXkWt9vIy6-SpjhQoX38iR9CENoVucsJfw5dqa7Dg8aYvr5Dq23QcnJc6oizYBUn-AgIAkaeCgHZTlluvxMJXKBXtcN6bhiMQ&x-client-SKU=ID_NETSTANDARD2_0&x-client-ver=6.7.1.0

Do you have any idea? What I have to change?

This is the configuration of my IdentityServer4

Basic

63397-identityserver4-genericwebsite-basic.png

Authentication

63446-identityserver4-genericwebsite-authentication.png

Token

63465-identityserver4-genericwebsite-token.png

Here the video of what it is happening.

Microsoft Identity Manager
Microsoft Identity Manager
A family of Microsoft products that manage a user's digital identity using identity synchronization, certificate management, and user provisioning.
700 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,573 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.