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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…