gpt4 book ai didi

c# - authCookie 在 global.asax 中不安全

转载 作者:太空宇宙 更新时间:2023-11-03 11:45:59 25 4
gpt4 key购买 nike

我有一个登录问题。

首先,我在登录时使用 SSL。

当我登录时,我正在创建一个这样的 cookie。当我检查它是否安全时,答案是肯定的。

FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,                          // version
UserName.Text, // user name
DateTime.Now, // creation
DateTime.Now.AddMinutes(60),// Expiration
false, // Persistent
role); // User data

// Now encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

// Create a cookie and add the encrypted ticket to the
// cookie as data.
HttpCookie authCookie =
new HttpCookie(FormsAuthentication.FormsCookieName,
encryptedTicket);

if (authCookie.Secure)
{
new GUIUtility().LogMessageToFile("The cookie is secure with SSL.");
// Add other required code here.
}

authCookie.Secure = FormsAuthentication.RequireSSL;

// Add the cookie to the outgoing cookies collection.
HttpContext.Current.Response.Cookies.Add(authCookie);

// Redirect the user to the originally requested page
Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text,false));

然后这被重定向到具有以下代码的 global.asax 页面:

string cookieName = FormsAuthentication.FormsCookieName.ToString();
HttpCookie authCookie = Context.Request.Cookies[cookieName];

try
{
new GUIUtility().LogMessageToFile(cookieName + authCookie.Secure);
}
catch (Exception)
{
//
}

在这里,我得到的 cookieName 为“.ASPXAUTH”,authCookie.Secure 值为 False。为什么会发生这种情况,我希望此处的 authCookie.Secure 值为真。

有什么建议吗?谢谢

我的网络配置是这样的:

<authentication mode="Forms">
<forms loginUrl="Login.aspx" defaultUrl="~/Default.aspx" slidingExpiration="true" timeout="120" path="/" requireSSL="true" protection="All">
</forms>
</authentication>
<httpCookies requireSSL="true"/>
<authorization>
<deny users="?"/>
<!--<allow users="*"/>-->
</authorization>

最佳答案

限制身份验证 Cookie 到 HTTPS 的连接

Cookie 支持“安全”属性,该属性决定浏览器是否应将 cookie 发送回服务器。通过设置安全属性,浏览器仅将 cookie 发送到使用 HTTPS URL 请求的安全页面。

如果您使用的是 .NET Framework 1.1 版,请在元素上使用 requireSSL="true"设置安全属性,如下所示:

<forms loginUrl="Secure\Login.aspx"
requireSSL="true" . . . />

如果您使用的是 .NET Framework 1.0 版,请使用以下代码在 Global.asax 的 Application_EndRequest 事件处理程序中手动设置安全属性:

protected void Application_EndRequest(Object sender, EventArgs e) 
{
string authCookie = FormsAuthentication.FormsCookieName;

foreach (string sCookie in Response.Cookies)
{
if (sCookie.Equals(authCookie))
{
// Set the cookie to be secure. Browsers will send the cookie
// only to pages requested with https
Response.Cookies[sCookie].Secure = true;
}

}

所以根据我的说法,第一个选项在网络配置中不起作用,所以我手动进行这是代码中的第二个选项..

请提出建议。

关于c# - authCookie 在 global.asax 中不安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3428556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com