我有一个 Asp .net core MVC 应用程序。它连接到 Identity Server 4 进行身份验证。托管在 Docker Swarm 上。
MVC 应用程序托管在 https://XXXXXXX 上。
ConfigurServies
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
//options.DataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"C:\temp-keys\"));
// when the identity has been created from the data we receive,
// persist it with this authentication scheme, hence in a cookie
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
// Identity server endpoint
options.Authority = settingsSetup.IdentityServerEndpoint;
// Client id to login with
options.ClientId = settingsSetup.ClientId;
// Client secret.
options.ClientSecret = settingsSetup.Secret;
// Scope of our API
options.Scope.Add("testapi");
options.Scope.Add("devconsole");
// adding offline_access to get a refresh token
options.Scope.Add("offline_access");
options.ResponseType = "code id_token";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
});
当我尝试运行应用程序时,出现了重定向URI不匹配的错误。Invalid redirect_uri: http://developerconsole.XXXXX.io/signin-oidc
{
"ClientId": "BB1D2DA8-D7E4-4AF5-94FA-19EAD6B7D711.apps.XXXXX.biz",
"ClientName": "Developer Console",
"AllowedRedirectUris": [
"http://localhost:55000/signin-oidc",
"http://localhost:55000/auth.html",
"http://localhost:55000/auth-silent.html"
"https://developerconsole.XXXXX.io/signin-oidc"
],
"SubjectId": "21379983",
"RequestedScopes": "",
"Raw": {
"client_id": "BB1D2DA8-D7E4-4AF5-94FA-19EAD6B7D711.apps.XXXXX.biz",
"redirect_uri": "http://developerconsole.XXXXX.io/signin-oidc",
"response_type": "code id_token",
"scope": "openid profile testapi devconsole offline_access",
"response_mode": "form_post",
"nonce": "636625889658410682.MjNlMmQwNjgtZmY0MC00MmVkLWFiNmMtN2M2YmQ5YTM5ZTQ3NjFiYzI2ZjktZWM0Yi00NDk3LTk1ZWMtNjJkYjViMDYwMTJm",
"state": "CfDJ8Pwa8A3ipXlKtuyxNMpMxAz5QUFmdSunRKdlKS9sS390AKp8gIUZShQUMMCkFAhYLytitgsXUBgwlQDJaJvtHFqzHygLCPwS8Jab6IJzhpry90qS51E1y_eRlppamRDOzYDZ6fcDFzWV1U43BTP2B6pnPTSLNcZRaooyGBXtNokeUqOJ--u-_MOQB8Bw3n2cRyV4kisHNkslD1Gsi2wn1Cx6aTVlqzw_pxHelAXm1P8FyDJpD7G0azFgKgpQF0DRJtC5penRJQzHIHvQN8v4ECGeuSD1zlyfJYClLO2r6kY_R2OYqtBkV0r_SNc9h7xUYmnVaHKQzYqVc_mJO4iLLSMTZrBUICZWR8c4PZw0Os3N",
"x-client-SKU": "ID_NET",
"x-client-ver": "2.1.4.0"
}
}
错误是由于我将"https://developerconsole.XXXXX.io/signin-oidc"
作为重定向uri,而不是"http://developerconsole.XXXXX.io/signin-oidc"
导致的。 我不想添加HTTP重定向URI。为什么我的应用程序生成的重定向URI是http而不是https?
如果我确实添加了HTTP,我会收到一个非常烦人的Correlation错误。 我认为这是由于服务器返回https而不是http造成的,因为服务器自动将http转换为https。
未处理的异常发生在处理请求时。 例外:关联失败。 Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler + d__12.MoveNext()
堆栈查询cookie标头异常:关联失败。 Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler + d__12.MoveNext() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 任务) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware + d__6.MoveNext() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 任务) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware + d__7.MoveNext()
我可能不需要提到这在本地主机上运行正常:/
Microsoft.AspNetCore.HttpOverrides
中的转发标头中间件可能会有所帮助。请在此处查看:https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.0 - spendersettingsSetup.IdentityServerEndpoint
是否正确吗? - penleychan