我在使用扫描软件时遇到了中等安全问题 [Cookie 安全性:持久性 Cookie (4728)]
看来我使用了持久性cookie。我发现一些答案告诉不要设置过期,但我正在使用System.Web.Security。因此我必须将过期设置如下,否则课程会出错。FormsAuthenticationTicket.cs 是一个临时类,我无法编辑该类。
有没有办法将 .ASPXAUTH cookie 设置为会话 cookie?
不管 authTicket.expiration 是 30 分钟还是 1 小时,ASPXAUTH cookie 的到期时间仍然是一天。如何让它发挥作用?
我设置了 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);
网络配置:
<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 设置