[英]asp.net secure cookies
我想保護我的 cookie,我閱讀了 ASP.NET_SessionId cookie 的“HTTPOnly”和“Secure”cookie 標志。 我在 VS 中創建了新的 asp.net 項目。 在 Inspectors -> raw 的提琴手中,我有:
Cookie: DXCurrentThemeMVC=Office2010Black; ASP.NET_SessionId=1gq0t1mi234xyljqnxrzbqfx
然后我修改 web.config :
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpCookies httpOnlyCookies="true" requireSSL="true"/>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" requireSSL="true" />
</authentication>
但在提琴手相同的數據
Cookie: DXCurrentThemeMVC=Office2010Black; ASP.NET_SessionId=1gq0t1mi234xyljqnxrzbqfx
我想當我添加<httpCookies httpOnlyCookies="true" requireSSL="true"/>
時,我在提琴手中看不到 cookie,或者 cookie 將被加密。 這是正確的結果嗎? 或者我在某個地方有錯誤?
編輯
以及為什么我在提琴手中看不到
Set-Cookie: ASP.NET_SessionId=ig2fac55; path=/; secure; HttpOnly
但只有沒有設置和安全的cookie,而且HttpOnly也在firebug中我看到了相同的結果
EDIT2似乎我發現了我的問題:我在 iis 上托管應用程序並在 firebug 中查找 cookie,並且我有帶有安全和 httpOnly 標志的 cookie:
ASP.NET_SessionId=98sfd90sdf89sd0f80s8; path=/; secure; HttpOnly
查看 MSDN 中的httpCookies Element會話。
httpOnlyCookies在響應標頭中設置 HttpOnly 標志。 請參閱保護您的 Cookie:HttpOnly文章。
requireSSL強制 cookie 通過安全通道傳輸,因此在傳輸過程中它不會被刪除並被加密。
要保護 Cookie,您可以使用IDataProtector加密和解密您的值
在你的班級:
private IDataProtector dataProtector;
public HomeController(IDataProtectionProvider provider)
{
//TODO:Set Your Private Key
dataProtector = provider.CreateProtector("YourPrivateKey");
}
你可以在你的方法中使用它:
public IActionResult SetNewCookie(string key,string value) {
var cookieOptions = new CookieOptions();
cookieOptions.Secure = true;
cookieOptions.HttpOnly = true;
HttpContext.Response.Cookies.Append(key,dataProtector.Protect(value),cookieOptions);
return View(nameof(Index));
}
最后描述我們可以使用的cookie值:
dataProtector.Unprotect(value);
我還在使用“ CookieOptions ”並設置 secure 和 httpOnly true 以獲得更高的安全性(當您使用第三方軟件時,獲取 cookie 會更加困難)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.