【问题标题】:Unable to set path ASP.NET_sessionid cookie无法设置路径 ASP.NET_sessionid cookie
【发布时间】:2021-10-31 04:53:29
【问题描述】:

您好,我需要认真的帮助 我已经尝试了所有方法,但无法更改 ASP.NET_sessionid cookie 的路径 它的路径始终设置为“/”,我想将其设置为文件夹或目录

这个问题需要解决,因为它是由应用安全团队提出的 试过了,iis重写规则,自定义会话id管理器 非常感谢任何帮助

【问题讨论】:

    标签: c# asp.net asp.net-mvc api asp.net-core


    【解决方案1】:

    正如@iamdln 所说,您需要创建自己的 SessionIDManager,但您还需要在 Web.config 上对其进行配置。

    它对我有用。

    你的 SessionIdManager 类

    public class MySessionIDManager : SessionIDManager, ISessionIDManager
    {
        void ISessionIDManager.SaveSessionID(HttpContext context, string id, out bool redirected, out bool cookieAdded)
        {
            base.SaveSessionID(context, id, out redirected, out cookieAdded);
    
            if (cookieAdded)
            {
                var name = "ASP.NET_SessionId";
                var cookie = context.Response.Cookies[name];
                cookie.Path = "/yourPath";
            }
        }
    }
    

    Web.config,替换你的命名空间和类。 这进入 部分。

    <sessionState sessionIDManagerType = "Namespace.MySessionIDManager"></sessionState>
    

    原始链接:

    ASP.NET Forum - Explains how to override path

    StackOverFlow - Explains how to override domain

    无论如何,两者都非常相似。

    【讨论】:

      【解决方案2】:

      您需要创建自己的SessionIDManager,它继承自 ISessionIDManager,并将cookie.Path 更改为您想要的任何内容。

      static HttpCookie CreateSessionCookie(String id) {
          HttpCookie  cookie;
      
          cookie = new HttpCookie(Config.CookieName, id);
          cookie.Path = "/";
          cookie.SameSite = Config.CookieSameSite;
      
          // VSWhidbey 414687 Use HttpOnly to prevent client side script manipulation of cookie
          cookie.HttpOnly = true;
      
          return cookie;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-12
        • 1970-01-01
        • 2017-01-30
        • 1970-01-01
        • 2020-07-18
        • 2014-02-15
        • 2012-03-03
        相关资源
        最近更新 更多