【发布时间】:2019-12-30 11:00:18
【问题描述】:
我有严格的基础架构,无法更改并尝试启用 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"]}");
}
}
【问题讨论】:
标签: angular asp.net-web-api identityserver4 saml-2.0 idp