我正在使用带有OpenID连接的APS.Net Core 2.1 MVC应用程序,使用授权流程进行连接。运行以下配置时,出现以下错误。我尝试指定重定向uri并更改addauthentication中的字段,但未成功。在下面找到我的startup.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
//services.Configure<CookiePolicyOptions>(options =>
//{
// options.CheckConsentNeeded = context => true;
// options.MinimumSameSitePolicy = SameSiteMode.None;
//});
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddMemoryCache();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie("Cookies", options => {
options.Events.OnRedirectToLogin = context =>
{
context.Response.Headers["Location"] = "<host url>";
return Task.CompletedTask;
};
})
.AddOpenIdConnect("oicd", options =>
{
options.Scope.Add("openid");
options.ResponseType = OpenIdConnectResponseType.Code;
options.ClientId = "<client id>";
options.SignInScheme = "Cookies";
options.CallbackPath = "/";
options.GetClaimsFromUserInfoEndpoint = true;
options.Authority = "<authority url>";
options.SaveTokens = true;
options.RequireHttpsMetadata = false;
options.Scope.Clear();
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
//app.UseHttpsRedirection();
app.UseAuthentication();
app.UseStaticFiles();
app.Use((context, next) =>
{
context.Request.Scheme = "https";
return next();
});
//app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
但是出现以下错误:
warn:
Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler[14]
.AspNetCore.Correlation. state property not found.
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
System.Exception: An error was encountered while handling the remote login.
---> System.Exception: Correlation failed.
答案 0 :(得分:0)
如果要动态更改重定向URL。您可以在OIDC中间件的Notifications属性中动态更改它-查看RedirectingToIdentityprovider
回调和通知参数的Protocol属性:
options.Events.OnRedirectToIdentityProvider = async n =>
{
n.ProtocolMessage.RedirectUri = "YourUrl";
await Task.FromResult(0);
};
Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler [14] .AspNetCore.Correlation。找不到国家财产。
您在DI中是否有多个OIDC处理程序,并且没有为每个处理程序设置唯一的回调路径?如果您使用的是http网址,也请尝试使用https网址。