停止 IResponseCookies.Append() 对 cookie 进行编码。点网核心 2.0
Stop IResponseCookies.Append() from encoding cookies. dotnetcore 2.0
我们正在构建一个 dotnetcore 2.0 网络应用程序,它通过 http 从遗留系统接收 cookies 并在响应中设置 cookies。
当我调试应用程序时,我可以看到 cookie.value
不是 url 编码的,但是在调用附加后 set-cookie headers 是 url 编码的.这是有问题的,因为遗留 cookie 需要未编码,因为遗留应用程序会按原样读取它们。
public static void AddCookie(this IResponseCookies cookies, Cookie cookie)
{
cookies.Append(cookie.Name, cookie.Value, new CookieOptions
{
Domain = cookie.Domain,
Expires = cookie.Expires == DateTime.MinValue ? null : (DateTimeOffset?) cookie.Expires,
HttpOnly = cookie.HttpOnly,
Path = cookie.Path,
Secure = cookie.Secure
});
}
有没有办法防止它们被编码?旗帜或设置某处?
我尝试编写一个 owin 中间件,但 set-cookie header 始终为空。
public class CookieDecode
{
private readonly RequestDelegate _next;
public CookieDecode(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
var x = context.Response.Headers;
var y = x["set-cookie"];
//y is always empty
await _next.Invoke(context);
}
}
想法?
不,没有避免 URL 编码的选项。但是,here is what Append
是这样做的。您可以像他们一样手动设置页眉吗?
public void Append(string key, string value, CookieOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
var setCookieHeaderValue = new SetCookieHeaderValue(
Uri.EscapeDataString(key),
Uri.EscapeDataString(value))
{
Domain = options.Domain,
Path = options.Path,
Expires = options.Expires,
MaxAge = options.MaxAge,
Secure = options.Secure,
SameSite = (Net.Http.Headers.SameSiteMode)options.SameSite,
HttpOnly = options.HttpOnly
};
var cookieValue = setCookieHeaderValue.ToString();
Headers[HeaderNames.SetCookie] = StringValues.Concat(Headers[HeaderNames.SetCookie], cookieValue);
}
我们正在构建一个 dotnetcore 2.0 网络应用程序,它通过 http 从遗留系统接收 cookies 并在响应中设置 cookies。
当我调试应用程序时,我可以看到 cookie.value
不是 url 编码的,但是在调用附加后 set-cookie headers 是 url 编码的.这是有问题的,因为遗留 cookie 需要未编码,因为遗留应用程序会按原样读取它们。
public static void AddCookie(this IResponseCookies cookies, Cookie cookie)
{
cookies.Append(cookie.Name, cookie.Value, new CookieOptions
{
Domain = cookie.Domain,
Expires = cookie.Expires == DateTime.MinValue ? null : (DateTimeOffset?) cookie.Expires,
HttpOnly = cookie.HttpOnly,
Path = cookie.Path,
Secure = cookie.Secure
});
}
有没有办法防止它们被编码?旗帜或设置某处?
我尝试编写一个 owin 中间件,但 set-cookie header 始终为空。
public class CookieDecode
{
private readonly RequestDelegate _next;
public CookieDecode(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
var x = context.Response.Headers;
var y = x["set-cookie"];
//y is always empty
await _next.Invoke(context);
}
}
想法?
不,没有避免 URL 编码的选项。但是,here is what Append
是这样做的。您可以像他们一样手动设置页眉吗?
public void Append(string key, string value, CookieOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
var setCookieHeaderValue = new SetCookieHeaderValue(
Uri.EscapeDataString(key),
Uri.EscapeDataString(value))
{
Domain = options.Domain,
Path = options.Path,
Expires = options.Expires,
MaxAge = options.MaxAge,
Secure = options.Secure,
SameSite = (Net.Http.Headers.SameSiteMode)options.SameSite,
HttpOnly = options.HttpOnly
};
var cookieValue = setCookieHeaderValue.ToString();
Headers[HeaderNames.SetCookie] = StringValues.Concat(Headers[HeaderNames.SetCookie], cookieValue);
}