KeyNotFoundException:字典中不存在给定键 'OpenIdConnect.Code.RedirectUri'

KeyNotFoundException: The given key 'OpenIdConnect.Code.RedirectUri' was not present in the dictionary

我正在将 vue cli 中间件与带有混合流的身份服务器一起使用。 在 Ajax 调用中,我已手动重定向到 identityprovider,如下所示。

options.Events.OnRedirectToIdentityProvider = context =>
                 {
                     if (context.Request.Path.StartsWithSegments("/api"))
                     {
                         if (context.Response.StatusCode == (int)HttpStatusCode.OK)
                         {
                             context.ProtocolMessage.State = options.StateDataFormat.Protect(context.Properties);
                             context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                             context.Response.Headers["Location"] = context.ProtocolMessage.CreateAuthenticationRequestUrl();
                         }
                         context.HandleResponse();
                     }
                     return Task.CompletedTask;
                 };

从 identityserver 回调时,出现以下错误。

KeyNotFoundException: 字典中不存在给定键 'OpenIdConnect.Code.RedirectUri'。

下面是调用堆栈。

Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler.HandleRequestAsync() Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext 上下文) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext 上下文) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext 上下文) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)

怎么了?

我正在使用来自以下 repo 的身份服务器。

https://github.com/IdentityServer/IdentityServer4

我的分叉回购(上面的代码)

https://github.com/hnviradiya/asp-net-core-vue-starter

options.Events.OnRedirectToIdentityProvider = redirectContext =>
                      {
                          if (redirectContext.Request.Path.StartsWithSegments("/api"))
                          {
                              if (redirectContext.Response.StatusCode == (int)HttpStatusCode.OK)
                              {
                                  redirectContext.Properties.RedirectUri = $"{redirectContext.Request.Scheme}://{redirectContext.Request.Host}{redirectContext.Request.PathBase}";
                                  redirectContext.Properties.Items.Add(OpenIdConnectDefaults.RedirectUriForCodePropertiesKey, redirectContext.ProtocolMessage.RedirectUri);
                                  redirectContext.ProtocolMessage.State = options.StateDataFormat.Protect(redirectContext.Properties);
                                  redirectContext.Response.StatusCode =   (int)HttpStatusCode.Unauthorized;
                                  redirectContext.Response.Headers["Location"] = redirectContext.ProtocolMessage.CreateAuthenticationRequestUrl();
                              }
                              redirectContext.HandleResponse();
                          }
                          return Task.CompletedTask;
                      };