【问题标题】:Persistent Cookie ( 4728 ) How to set .ASPXAUTH cookie's expires as session cookie?Persistent Cookie (4728) 如何将 .ASPXAUTH cookie 设置为会话 cookie?
【发布时间】:2022-02-14 10:30:35
【问题描述】:

我在使用扫描软件时遇到了中等安全问题 [Cookie 安全性:持久 Cookie (4728)]

看来我使用了持久性 cookie。 我发现一些答案告诉我不要设置过期,但我使用的是 System.Web.Security。因此我必须如下设置过期时间,否则课程会出错。 FormsAuthenticationTicket.cs 是一个临时类,我无法编辑该类。

  1. 有没有办法将 .ASPXAUTH cookie 设置为会话 cookie?

  2. 不管 authTicket.expiration 是 30 分钟还是 1 小时,ASPXAUTH cookie 的过期时间仍然是一天。如何让它发挥作用?

  3. 我将IIS设置为cookie的有效期为30分钟,cookie在正确的时间消失,但​​浏览器cookie的有效期仍然是一天。

这是我的 FormsAuthenticationTicket 代码:

控制器:

ar authTicket = new FormsAuthenticationTicket( 
                    version: 1,
                    name: _result.UserName,
                    issueDate: DTnow, DateTime
                    expiration:DTnow.AddHours(1),  
                    isPersistent: false, 
                    userData: _result.UserRank.ToString(), 
                    cookiePath: FormsAuthentication.FormsCookiePath
                    ); 
 var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket))
                {  
                    HttpOnly = true,
                    Secure = true  
                };

authCookie.Expires = DTnow.AddMinutes(30); 
if (authTicket.IsPersistent)
                {
                    authCookie.Expires = authTicket.Expiration;   
                    authCookie.Expires = DateTime.MinValue;   
                }
                Response.Cookies.Add(authCookie);

Web.config:

<system.web>
      <sessionState timeout="31"></sessionState>
<authentication mode="Forms">
  <forms loginUrl="~/AccountUser/Login" />
</authentication>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />
  <httpCookies httpOnlyCookies="true" requireSSL="true" /></system.web>

全球.asax:

protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        if (HttpContext.Current.User == null) return;
        if (HttpContext.Current.User.Identity.IsAuthenticated == false) return;
        if (Request.IsAuthenticated == false) return;

        FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
        FormsAuthenticationTicket authTicket = id.Ticket;
        string[] arrRolles = authTicket.UserData.Split(',');
        HttpContext.Current.User = new GenericPrincipal(HttpContext.Current.User.Identity, arrRolles);
    }

这是我的 IIS 设置

【问题讨论】:

    标签: session-cookies .aspxauth formsauthenticationticket


    【解决方案1】:

    我找到了答案。这是我的解决方案:

    首先,在Controller中不要设置“authCookie.Expires”。我已经设置了“authCookie.Expires = DateTime.MinValue”,但这也行不通!

    authCookie.Expires = DTnow.AddMinutes(30);  ---delete it ! 
    

    其次,在Controller中FormsAuthenticationTicket的属性isPersistent应该为false

    FormsAuthenticationTicket( 
                    version: 1,
                    name: _result.UserName,
                    issueDate: DTnow, DateTime
                    expiration:DTnow.AddHours(1),  
                    isPersistent: false, 
                    userData: _result.UserRank.ToString(), 
                    cookiePath: FormsAuthentication.FormsCookiePath
                    ); 
    

    第三,检查Web.config设置,

    <forms cookieless="UseDeviceProfile" loginUrl="~/AccountUser/Login" name=".ASPXAUTH" requireSSL="false" slidingExpiration="false" timeout="30" />
    

    应该是:

    <forms  loginUrl="~/AccountUser/Login" name=".ASPXAUTH" slidingExpiration="false" timeout="30" />
    

    【讨论】:

      猜你喜欢
      • 2013-05-01
      • 2017-02-04
      • 2015-12-27
      • 2011-12-13
      • 1970-01-01
      • 2011-02-10
      • 2019-11-18
      • 2020-11-08
      • 2013-11-19
      相关资源
      最近更新 更多