2009-11-22 29 views
16

所以我很困惑,因爲msdn和其他教程告訴我使用HttpCookies通過Response.Cookies.Add(cookie)添加cookie。但這是問題所在。 Response.Cookies.Add只接受Cookies和不HttpCookies,我得到這個錯誤:HttpCookie和Cookie的區別?

無法從「System.Net.CookieContainer」到「System.Net.Cookie」

此外,什麼樣的反應之間的區別轉換.Cookies.Add(cookie)和Request.CookieContainer.Add(cookie)?

感謝您的幫助,我試圖用C#教自己。

// Cookie 
Cookie MyCookie = new Cookie(); 
MyCookie.Name = "sid"; 
MyCookie.Value = SID; 
MyCookie.HttpOnly = true; 
MyCookie.Domain = ".domain.com"; 

// HttpCookie 
HttpCookie MyCookie = new HttpCookie("sid"); 
MyCookie.Value = SID; 
MyCookie.HttpOnly = true; 
MyCookie.Domain = ".domain.com"; 

Response.Cookies.Add(MyCookie); 

回答

13

您正在使用System.Net.HttpWebResponse。但上面的例子使用System.Web.HttpResponse,它以System.Web.HttpCookie作爲參數。

斯科特 - 阿倫

System.Web.HttpRequest is a class used on the server and inside an ASP.NET application. It represents the incoming request from a client.

System.Net.HttpWebRequest is a class used to make an outgoing request to a web application.

+2

那麼接下來的問題就是如何抓住一個從傳入請求身份驗證Cookie傳遞到傳出請求? – Blairg23 2016-03-09 18:25:12