2015-09-16 17 views
6

tôi đã thiết lập như sau trong web.config:Làm cách nào để đặt cờ 'an toàn' cho cookie trong trang web ASP.NET MVC?

<system.web> 
    <httpCookies httpOnlyCookies="true" requireSSL="true" /> 
</system.web> 

Khi tôi nhấn trang web sử dụng một kết nối HTTP, nó chuyển hướng đến trang đăng nhập của tôi (ghi rõ chương trình như HTTPS). Khi trình duyệt tìm nạp trang này, phản ứng đặt ra một số cookie (phiên Cookie ASP.NET, và yêu cầu xác minh dấu hiệu cho mẫu đăng nhập của tôi):

Set-Cookie: __RequestVerificationToken = IHx8a2zQU374d5CtsoEVW ... YtIc1; path = /; HttpOnly Set-Cookie: ASP.NET_SessionId = pfbkkxx2seqhdrxxiodxfbmh; path = /; HttpOnly

Những có HttpOnly cờ, đó là tốt - nhưng họ không có secure cờ như mô tả here on Wikipedia.

Nếu tôi sau đó đăng nhập, một cookie xác thực được tạo ra, và điều này không có bộ secure cờ:

Set-Cookie: MyWebSite.Authentication = RE3UD ... BDW4; path = /; an toàn; HttpOnly

Làm cách nào để đảm bảo rằng cờ secure được đặt trên tất cả cookie của tôi?


CẬP NHẬT: theo yêu cầu, đây là sản phẩm cURL tôi nhận được (khi lấy trang đăng nhập trực tiếp):

curl https://www.mywebsite.com/Account/Login --verbose --insecure 

cho:

% Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 
    0  0 0  0 0  0  0  0 --:--:-- --:--:-- --:--:--  0 
* Trying 194.73.98.116... 
* Connected to www.mywebsite.com (111.11.11.111) port 443 (#0) 
* ALPN, offering http/1.1 
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH 
* TLSv1.2 (OUT), TLS header, Certificate Status (22): 
} [5 bytes data] 
* TLSv1.2 (OUT), TLS handshake, Client hello (1): 
} [512 bytes data] 
* TLSv1.2 (IN), TLS handshake, Server hello (2): 
{ [85 bytes data] 
* TLSv1.2 (IN), TLS handshake, Certificate (11): 
{ [2618 bytes data] 
* TLSv1.2 (IN), TLS handshake, Server key exchange (12): 
{ [401 bytes data] 
* TLSv1.2 (IN), TLS handshake, Server finished (14): 
{ [4 bytes data] 
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16): 
} [138 bytes data] 
* TLSv1.2 (OUT), TLS change cipher, Client hello (1): 
} [1 bytes data] 
* TLSv1.2 (OUT), TLS handshake, Finished (20): 
} [16 bytes data] 
* TLSv1.2 (IN), TLS change cipher, Client hello (1): 
{ [1 bytes data] 
* TLSv1.2 (IN), TLS handshake, Finished (20): 
{ [16 bytes data] 
* SSL connection using TLSv1.2/ECDHE-RSA-AES256-SHA384 
* ALPN, server did not agree to a protocol 
* Server certificate: 
* subject: OU=Domain Control Validated; CN=*.mywebsite.com 
* start date: 2015-07-29 13:37:38 GMT 
* expire date: 2018-07-29 13:37:38 GMT 
* issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.; OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate Authority - G2 
* SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway. 
} [5 bytes data] 
> GET /Account/Login HTTP/1.1 
> Host: www.mywebsite.com 
> User-Agent: curl/7.43.0 
> Accept: */* 
> 
{ [5 bytes data] 
< HTTP/1.1 200 OK 
< Cache-Control: no-cache, no-store, must-revalidate 
< Pragma: no-cache 
< Content-Type: text/html; charset=utf-8 
< Expires: -1 
< Server: Microsoft-IIS/8.5 
< Strict-Transport-Security: max-age=31536000; includeSubdomains; preload 
< X-Frame-Options: Deny 
< X-Content-Type-Options: nosniff 
< X-XSS-Protection: 1; mode=block 
< Content-Security-Policy: default-src 'self';script-src 'self' www.google-analytics.com www.googletagmanager.com;object-src 'none';style-src 'self' fonts.googleapis.com;img-src 'self' www.google-analytics.com placehold.it placeholdit.imgix.net data:;media-src 'none';frame-src 'none';font-src 'self' fonts.gstatic.com;connect-src 'self';base-uri 'self';child-src 'none';frame-ancestors 'none';report-uri /WebResource.axd?cspReport=true 
< X-Frame-Options: SAMEORIGIN 
< X-Frame-Options: SAMEORIGIN 
< Set-Cookie: __RequestVerificationToken=bPWxIp8e4F4I0Jt26t5oZyvDM6059tAWSRbgc-b6Df5IMjyYFDD9fJKgRsKVjbtN3EGgtFuHcf1sTjlYSwDWgnlhSUuNW1q5yv3cGMxmEwE1; path=/; HttpOnly 
< Date: Fri, 04 Dec 2015 10:03:35 GMT 
< Content-Length: 12596 
< 
{ [12596 bytes data] 
100 12596 100 12596 0  0 31101  0 --:--:-- --:--:-- --:--:-- 31101 
* Connection #0 to host www.mywebsite.com left intact 
+0

Những loại cờ được gửi xuống từ máy chủ một cách chính xác trên lần đầu tiên bạn nhận được cookie. Vì vậy, bạn có thể vui lòng thêm phản hồi HTTP từ đầu ra curl? –

+0

@MehmetInce: Tôi đã thêm đầu ra cURL theo yêu cầu. –

+0

Thật kỳ lạ, mã web.config của bạn hoạt động tốt cho tôi. – nmit026

Trả lời

6

Các gợi ý con đường xung quanh điều này là để bảo đảm ID phiên và cookie yêu cầu biểu mẫu khi xử lý yêu cầu trang, ví dụ:

// This code will mark the forms authentication cookie and the 
// session cookie as Secure. 
if (Response.Cookies.Count > 0) 
{ 
    foreach (string s in Response.Cookies.AllKeys) 
    { 
     if (s == FormsAuthentication.FormsCookieName || s.ToLower() == "asp.net_sessionid") 
     { 
      Response.Cookies[s].Secure = true; 
     } 
    } 
} 

cũng như một dòng bổ sung trong webconfig cho đảm bảo thẻ hình thức auth:

<authentication mode="Forms"> 
    <forms ... requireSSL="true" /> 
</authentication> 

Nguồn: Securing Request-Response cookies-Secure forms authentication via Web.config

+0

Cảm ơn, tôi sẽ thử điều này. Mặc dù tôi không sử dụng xác thực Forms (hoặc ít nhất, không được cấu hình thông qua web.config), câu trả lời này khiến tôi tìm kiếm một tùy chọn cấu hình tương tự trong mã thiết lập xác thực ** ASP.NET Identity **. Có vẻ như lớp 'CookieAuthenticationOptions' có thuộc tính' CookieHttpOnly' và 'CookieSecure', cái sau có vẻ đầy hứa hẹn. –

+0

@MalikKhalil: Tôi cầu xin sự khác biệt: https://msdn.microsoft.com/en-us/library/microsoft.owin.security.cookies.cookieauthenticationoptions(v=vs.113).aspx –

+0

Ah, hiểu rồi .. .Cảm ơn bạn đã sửa tôi @GaryMcGill –

Các vấn đề liên quan