c# - 停止 IResponseCookies.Append() 编码 cookie。点网核心 2.0

标签 c# asp.net asp.net-mvc .net-core owin

<分区>

我们正在构建一个 dotnetcore 2.0 网络应用程序,它通过 http 从遗留系统接收 cookies 并在响应中设置 cookies。

当我调试应用程序时,我可以看到 cookie.value 不是 url 编码的,但是在调用 append 之后,set-cookie header 是 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是在做。您可以像他们一样尝试手动设置 header 吗?

    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);
    }

关于c# - 停止 IResponseCookies.Append() 编码 cookie。点网核心 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48324037/

相关文章:

javascript - 我怎样才能让我的标签点击另一个按钮

asp.net - ASP.NET 网站的移动母版页

c# - 查看有很多组件。多个模型对象?

asp.net - 部署时自定义 JsonConverter 被忽略

c# - Request.GetResponseAsync() 在后台任务中不起作用,没有引发异常

c# - 只写入 listView 中的一列?

c# - 在多线程 UI 程序中获取 System.ArgumentOutOfRangeException

javascript - 我必须编写正则表达式,只允许 @#$% 作为特殊符号,而不允许其他符号,例如 ?><`~ 符号

asp.net - 如何将 .aspx 页面添加到现有的 MVC 4 项目?

c# - ASP.NET MVC 将 List<Foo> 集合从我的 View 发送到 Controller