0

我有严格的基础设施,不能改变并试图使 IdentityServer4 与 SAML2.0 断言和令牌一起工作。只有 API 可以暴露给互联网。工作流程是这样的: 1. 用户点击 Angular 应用程序 2. 用户被重定向到外部 SAML2.0 IDP 提供者,并进行身份验证。3. IDP 使用 SAML 断言重定向到 WebApi,然后重定向到 IS4 以进行验证和令牌生成 4. WebAPI 使用 get 请求将令牌发送到 Angular APP(这是问题)

当我第一次登录 IDP 时,WebApi 将 POST 消息发送到角度,这显然会导致错误。当我刷新浏览器时,工作流程以相同的方式进行,但不需要提供凭据,因为我已经在 IDP 中唱歌,WebApi 发送 GET 消息,Angular 获取令牌并保存它。

我尝试了多种解决方案,几个重定向操作,向响应添加标头,发送带有位置和 301 状态代码的 HttpResponseMessage。在发送原始 httpResponseMessage 时,我第一次收到 POST 或者只是 Json。

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> PostLogin()
        {
            var form = Request.Form;
            var samlResponse = form["SamlResponse"];

            using (HttpClient client = new HttpClient())
            {
                var response = await client.RequestTokenAsync(new TokenRequest
                {
                    Address = $"{_configuration["IdentityServer"]}/connect/token",
                    GrantType = "SamlResponse",
                    ClientId = "Client",
                    ClientSecret = "Client",
                    Parameters = { { "SamlResponse", samlResponse } }
                });

                var r = new HttpResponseMessage(HttpStatusCode.SeeOther);
                r.Headers.Location = new Uri($"{_configuration["webpage"]}/login?token={response.Json["access_token"]}");

                Response.Headers.Add(HeaderNames.Location, $"{_configuration["webpage"]}/login?token={response.Json["access_token"]}");

                return StatusCode(301);
            }

或者

                var r = new HttpResponseMessage(HttpStatusCode.SeeOther);
                r.Headers.Location = new Uri($"{_configuration["webpage"]}/login?token={response.Json["access_token"]}");

                return StatusCode(301, r);

或者

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> PostLogin()
        {
            var form = Request.Form;
            var samlResponse = form["SamlResponse"];

            using (HttpClient client = new HttpClient())
            {
               var response = await client.RequestTokenAsync(new TokenRequest
               {
                    Address = $"{_configuration["IdentityServer"]}/connect/token",
                    GrantType = "SamlResponse",
                    ClientId = "Client",
                    ClientSecret = "Client",
                    Parameters = { { "SamlResponse", samlResponse } }
                });

                if (response.IsError)
                {
                    return StatusCode(400, response.Error);
                }

                return Redirect($"{_configuration["webpage"]}/login?token={response.Json["access_token"]}");
            }
        }
4

1 回答 1

0

解决方案是在 chrome 上启用 chrome://flags/#allow-insecure-localhost 标志。红色证书导致刷新初始 POST 请求。之后一切顺利。

于 2019-09-24T13:59:55.987 回答