2016-10-07 3 views
0

私は、データを収集し、次の方法で、サーバー上にクッキーを設定するためにPOSTリクエストを使用します。読むクッキーHTTPS

HttpCookie cookies = new HttpCookie(name) { 
    Value = value, 
    Expires = DateTime.MaxValue, 
    Secure = true }; 
HttpContext.Current.Response.Cookies.Remove(name); 
HttpContext.Current.Response.Cookies.Add(cookies); 

POSTリクエストは、私はjsファイルを使用してページをリロードし終えた後。 私はそのページにcookieの値を使用します。

私は彼らが好き取得しよう:

if (HttpContext.Current.Request.Cookies[NameCookieName] != null) 
{ 
    name = HttpContext.Current.Request.Cookies[NameCookieName].Value; 
} 
if (HttpContext.Current.Request.Cookies[RegionCookieName] != null) 
{ 
    region = HttpContext.Current.Request.Cookies[RegionCookieName].Value; 
} 

を、私はそれが正常に動作しているHTTPを使用している場合。

私はHTTPS Cookieを使用していますが、私がそれらを読むとcoookieの値はNULLです。

HTTPSを使用してCookieを設定/取得するにはどうすればよいですか?

答えて

0

私はセキュリティ保護されたプロパティを使用する必要があると思います。 HttpCookieクラスのC#には、HTTPS/SSLでCookieを取得および設定できるようにするSecureプロパティがあります。私はあなたが設定し、それらのHTTPSページだけでなく、HTTPのものを介してクッキーを取得する条件を設定する必要がありますと信じています。

HTTPSを介して設定されたCookieは、HTTP経由でのみ動作するはずなので、Secureを使用してCookieを作成することもできます。これは設定されていないので、CookieはデフォルトでHTTPページにのみ設定されているように聞こえます。あなたは、コードの下に参照することができます

protected void Application_EndRequest(Object sender, EventArgs e) 
    { 
     string authCookie = FormsAuthentication.FormsCookieName; 

     foreach (string sCookie in Response.Cookies) 
     { 
      if (sCookie.Equals(authCookie)) 
      { 
       // Set the cookie to be secure. Browsers will send the cookie 
       // only to pages requested with https 
       var httpCookie = Response.Cookies[sCookie]; 
       if (httpCookie != null) httpCookie.Secure = true; 
      } 
     } 
    } 

は、それはあなたのお役に立てば幸いです。

関連する問題