问题描述
我需要在我的控制器方法之一中返回 HttpResponseMessage
并在少数情况下向其添加 cookie。
- How add Cookies to http request header in ASP .NET Core MVC
- HTTP Response Headers in ASP.NET Core
- HTTP Response Headers in ASP.NET Core
我使用过类似于以下内容的 .NET Framework 代码,但我需要在 .NET Core 中使用它:
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK,content);
if (!string.IsNullOrEmpty(cookietoken))
{
response.Headers.AddCookies(new[]
{
new CookieHeaderValue("MyCookie",cookietoken)
{
Expires = DateTimeOffset.Now.AddHours(4),Path = "/",HttpOnly = true,Secure = true,}
});
}
protected IActionResult CreateInternalServerResponse<T>(T data) =>
StatusCode((int)HttpStatusCode.InternalServerError,data);
var responseMessage =
CreateInternalServerResponse(
"Call to Api Failed. Response received: "
+ (jsonResp["message"]));
但我不确定如何添加 cookie。
解决方法
试试下面的代码:
...
<proxy>
<id>myproxy</id>
<active>true</active>
<protocol>http</protocol>
<username>username</username>
<password>{NG9kNfX1DQAHKQeA4ksUpLJvr2EzWWgGSHSZVE/T/sU=}</password>
<host>rproxy.url.com</host>
<port>3128</port>
<nonProxyHosts>localhost</nonProxyHosts>
</proxy>
...