2017-09-06 164 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'這是真的,即使我的本地連接不安全。 –