2017-09-06 102 views
0

我创建了一个asp.net项目 创建一个新的HttpCookie,我想安全标志添加到它时,我的连接是安全 下面是我的代码安全Cookie在ASP.NET中不添加安全标志

var responseCookie = new HttpCookie(Test) 
     { 
      HttpOnly = true, 
      Value = asdasdhoi234 
     }; 
     if (FormsAuthentication.RequireSSL && Request.IsSecureConnection) 
     { 
      responseCookie.Secure = true; 
     } 
     Response.Cookies.Set(Test); 

但cookie仍然不安全,我不理解问题。

在我的httpsHeaders中,它仍然没有显示我的安全cookie 我的域名是https,但我的cookie仍然不安全。

enter image description here

回答

0

new HttpCookieconstructor采用字符串作为参数。因此我想你的Test是一个字符串。您需要在实际的Cookie对象上设置Secure标志,而不是字符串。试试这个:

var responseCookie = new HttpCookie(Test) 
{ 
    HttpOnly = true, 
    Value = "asdasdhoi234", 
    Secure = FormsAuthentication.RequireSSL && Request.IsSecureConnection 
}; 
Response.Cookies.Set(responseCookie); 

此外,请确保您的web.config文件中包含requireSSL属性设置为true为docs说:

<authentication mode="Forms"> 
    <forms loginUrl="member_login.aspx" 
    cookieless="UseCookies" 
    requireSSL="true" 
    path="/MyApplication" /> 
</authentication> 
+0

对不起,这是一个错字,我有在实际的对象上设置标志,请检查我更新的问题 –

+0

@ParshuramKalvikatte请检查'FormsAuthentication.RequireSSL'是否为真(需要在您的web.config中更新) –

+0

我没有这个代码在web.config中,但我不要理解,而我正在调试'FormsAuthentication.RequireSSL && Request.IsSecureConnection'这是真的,即使我的本地连接不安全。 –