Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

openid connect - OpenIdConnectAuthenticationHandler: message.State is null or empty, when connecting to any OIDC Server;Tried several of them

I am using ASP.NET Core 5.0 and Microsoft.AspNetCore.Authentication libraries to develop an OIDC client-side implementation of Authorization code workflow. When I run the configured redirect path in the browser, I get the exceptions:

An unhandled exception occurred while processing the request. Exception: OpenIdConnectAuthenticationHandler: message.State is null or empty. Unknown location

Exception: An error was encountered while handling the remote login. Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler.HandleRequestAsync()

Here is my code. Do you see where I am going wrong?

public void ConfigureServices(IServiceCollection services)
{
  JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
  services.AddControllersWithViews();
  services.AddAuthentication(options =>
  {
    options.DefaultScheme = "cookie";
    options.DefaultChallengeScheme = "oidc";
  })
      .AddCookie("cookie", options =>
      {
        options.Cookie.Name = "mvccode";
        options.Cookie.SameSite = SameSiteMode.None;
      })
      .AddOpenIdConnect("oidc", options =>
      {
        options.Authority = "https://localhost:9443/oauth2/oidcdiscovery";
        options.ClientId = Configuration.GetValue<string>("WSO2:id");
        options.ClientSecret = Configuration.GetValue<string>("WSO2:secret");
        options.ResponseType = "code";
        options.UsePkce = true;
        options.CallbackPath = "/cb";
        options.Scope.Clear();
        options.Scope.Add("openid");
        options.GetClaimsFromUserInfoEndpoint = true;
        options.SaveTokens = true;
        options.TokenValidationParameters = new TokenValidationParameters
        {
          NameClaimType = "name",
          RoleClaimType = "role"
        };
      });
  services.AddAccessTokenManagement(options =>
  {
    options.Client.Scope = "api";
  })
      .ConfigureBackchannelHttpClient()
          .AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(new[]
          {
                    TimeSpan.FromSeconds(1),
                    TimeSpan.FromSeconds(2),
                    TimeSpan.FromSeconds(3)
          }));
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
  app.UseDeveloperExceptionPage();
  app.UseHttpsRedirection();
  app.UseStaticFiles();

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

  app.UseEndpoints(endpoints =>
  {
    endpoints.MapGet("/cb", async context =>
    {
      await context.ChallengeAsync();
      var toSend = new AdRequest().GetRequest(Configuration);
      ClaimsPrincipal principal = context.User;
      if (null != principal)
      {
        foreach (Claim claim in principal.Claims)
        {
          await context.Response.WriteAsync("CLAIM TYPE: " + claim.Type + "; CLAIM VALUE: " + claim.Value + "</br>");
        }
      }

      await context.Response.WriteAsync(toSend);
    });
    

  });

}
question from:https://stackoverflow.com/questions/66045320/openidconnectauthenticationhandler-message-state-is-null-or-empty-when-connect

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...