2015-09-16 5 views
6

Ich habe folgendes in web.config gesetzt:Wie kann ich das "sichere" Flag für Cookies in einer ASP.NET MVC-Website setzen?

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

Wenn ich die Website schlagen eine HTTP-Verbindung verwendet wird, leitet es automatisch auf die Anmeldungsseite (das Schema als HTTPS Angabe). Wenn der Browser diese Seite holt, setzt die Reaktion einige Cookies (die ASP.NET-Session-Cookie, und die Anfrage Ãœberprüfungscode für Anmeldungsformular):

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

Diese haben die HttpOnly Flagge, was gut ist - aber sie haben nicht die secure Flagge als here on Wikipedia beschrieben.

Wenn ich dann in einzuloggen, ein Authentifizierungs-Cookie wird erstellt, und diese hat haben die secure Flag gesetzt:

Set-Cookie: MyWebSite.Authentication = RE3UD ... BDW4; Pfad = /; sicher;

Httponly

Wie kann ich sicherstellen, dass die secure Flagge auf alle meine Cookies gesetzt ist?


UPDATE: wie gewünscht, ist dies die cURL Ausgabe erhalte ich (wenn die Login-Seite direkt zu holen):

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

gibt:

% 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

Diese Art von Flags werden nach unten vom Server korrekt auf gesendet das erste Mal, wenn Sie den Cookie erhalten. Könnten Sie bitte eine HTTP-Antwort von curl output hinzufügen? –

+0

@MehmetInce: Ich habe die cURL-Ausgabe wie gewünscht hinzugefügt. –

+0

Das ist seltsam, Ihr web.config-Code funktionierte gut für mich. – nmit026

Antwort

6

Die vorgeschlagene Weg, um Dies dient zum Sichern der Sitzungs-ID und der Formanforderungs-Cookies bei der Bearbeitung von Seitenanforderungen, z

// 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; 
     } 
    } 
} 

sowie eine zusätzliche Zeile in der webconfig zur Sicherung Form Auth-Token:

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

Quellen: Securing Request-Response cookies-Secure forms authentication via Web.config

+0

Danke, ich werde es versuchen. Obwohl ich die Formularauthentifizierung nicht verwende (oder zumindest nicht über web.config konfiguriert wurde), hat mich diese Antwort veranlasst, nach einer ähnlichen Konfigurationsoption innerhalb des Authentifizierungs-Setup-Codes ** ASP.NET Identity ** zu suchen. Es scheint, dass die 'CookieAuthenticationOptions' Klasse' CookieHttpOnly' und 'CookieSecure' Eigenschaften hat, wobei letzteres vielversprechend klingt. –

+0

@MalikKhalil: Ich bitte um zu unterscheiden: https://msdn.microsoft.com/en-us/library/microsoft.owin.security.cookies.cookieauthenticationoptions(v=vs.113).aspx –

+0

Ah, hab es .. . Vielen Dank für die Korrektur von @GaryMcGill –