Домен 1 отправляет HTTP-запрос к API в домене 2.
Заголовки HttpResponseMessage содержат «Set-Cookie», как показано ниже.
Как я могу прочитать этот файл cookie из ответа и прикрепить его к другому ответу, который будет перенаправлять на домен 2?
Спасибо!
.AspNetCore.Cookies = CfDJ8O9xThwMNOhChcuBECE1VXHz3M0rFuwIeZqKbZckvkDXflMWbJYxtVS12ImQCV7dbdHP9rRMp2JblrWTJMdRHvuOQ_kyYXQrU5DF98e4NEOLfrCOwYv0ReLCljm75A0IEIQOu2Moa4TV6SgE21T7RTFsvlY0YcGO6KjYZbfcHbZh5wCQNvFzMvWSkZ_woEgxdC9822gXAgOqMw5EoFkJPV4UujuiGuwqSHouwgG1DhVIHsvW47ICe6x4cT13suZKygNdIHkSqIS3h01Z5nyT5aucTcd-mwurFtrMJ-0SVnBkm5D32ZWbgijMDDcJVCKtdzxPJBuUYrcGi2thhKAgrC_PBB7G5XgH0QuFle_ZKhus74FO5QCYW81wb_-87aCZDGiijBgPtmID3yim_BGJnCVYbRdz9kyNEfE5RzW-6dl13wZGd7c9B25om_08xZmZnnQB8a - t742sybuMpmchvQExvM3w9dVkmq_tb9Bhrk0J_hbMH8HJ52GbKiFkpuKl1oxz8VHak-caXYjWo9z8kd5rJMdJOQhMJgn_SJ2tLkp6LtBnrCNGjhvIFtS3YHIiw; истекает = вт, 11 июня 2019 г. 12:09:42 мск; Путь = /; samesite = слабый; 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.
Ниже описано, как я сгенерировал cookie в API domian 2.
[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 });
}