如何阅读从asp.net核心API生成的“ Set-Cookie”?

时间:2019-05-28 12:46:05

标签: asp.net-core asp.net-core-webapi

域1将http请求发送到域2中的API。

HttpResponseMessage标头包含“ Set-Cookie”,如下所示。 如何从响应中读取此Cookie并将其附加到另一个将重定向到域2的响应? 谢谢!

.AspNetCore.Cookies = CfDJ8O9xThwMNOhChcuBECE1VXHz3M0rFuwIeZqKbZckvkDXflMWbJYxtVS12ImQCV7dbdHP9rRMp2JblrWTJMdRHvuOQ_kyYXQrU5DF98e4NEOLfrCOwYv0ReLCljm75A0IEIQOu2Moa4TV6SgE21T7RTFsvlY0YcGO6KjYZbfcHbZh5wCQNvFzMvWSkZ_woEgxdC9822gXAgOqMw5EoFkJPV4UujuiGuwqSHouwgG1DhVIHsvW47ICe6x4cT13suZKygNdIHkSqIS3h01Z5nyT5aucTcd-mwurFtrMJ-0SVnBkm5D32ZWbgijMDDcJVCKtdzxPJBuUYrcGi2thhKAgrC_PBB7G5XgH0QuFle_ZKhus74FO5QCYW81wb_-87aCZDGiijBgPtmID3yim_BGJnCVYbRdz9kyNEfE5RzW-6dl13wZGd7c9B25om_08xZmZnnQB8a - t742sybuMpmchvQExvM3w9dVkmq_tb9Bhrk0J_hbMH8HJ52GbKiFkpuKl1oxz8VHak-caXYjWo9z8kd5rJMdJOQhMJgn_SJ2tLkp6LtBnrCNGjhvIFtS3YHIiw; expires = Tue,2019年6月11日12:09:42 GMT;路径= /; samesite = lax; httponly

域1

public async void Start(string req, int id)
{
    HttpResponseMessage res = await PostHTTPResponse(string.Format("/API/Update?req={0}", req));

    IEnumerable<string> values;
    if (res.Headers.TryGetValues("Set-Cookie", out values))
    {
        // How to read the Set-Cookie here and attach it to the response object below?
    }

    var response = HttpContext.Current.Response;

    //Redirect to domain 2 with cookie attached to response.
    response.Redirect(string.Format("http://localhost:58379/Update?id={0}", id));
}

Domian 2是asp.net核心。 下面是我如何在domian 2 API中生成cookie。

[HttpPost]
[Route("Update")]
public async Task<IActionResult> Update(string req)
{
    List<Claim> claims = new List<Claim>();

    claims.Add(new Claim(Security.Claims.ClaimTypes.UserName,
                                 req));

    var claimsIdentity = new ClaimsIdentity(
      claims, CookieAuthenticationDefaults.AuthenticationScheme);

    var authProperties = new AuthenticationProperties
    {
        IsPersistent = true
    };

    await HttpContext.SignInAsync(
        CookieAuthenticationDefaults.AuthenticationScheme,
        new ClaimsPrincipal(claimsIdentity),
        authProperties);

    return this.Json(data: new { IsAuthenticated = this.User.Identity.IsAuthenticated });
}

0 个答案:

没有答案