Cookies是不保留任何数据,同时作为的WebRequest的一部分发送 [英] Cookies are not retaining any data while sent as part of a WebRequest

查看:76
本文介绍了Cookies是不保留任何数据,同时作为的WebRequest的一部分发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我如何建立饼干数据一起Web请求通过。当我使用招我看饼干没有数据和到期数据设置为1月1日检查从网站的响应,1900年任何帮助将是非常美联社preciated。请让我知道,如果事情是不是从以下code清晰,或者您需要更多的信息来回答我的问题。谢谢你。

  System.Net.Cookie用户等级和积分=
    新System.Net.Cookie(CUserType,用户,/,www.DOMAIN_NAME.com);
userType.Expires = DateTime.Now.AddYears(1);
System.Net.Cookie dUserType =
    新System.Net.Cookie(dCUserType,用户,/,www.DOMAIN_NAME.com);
dUserType.Expires = DateTime.Now.AddYears(1);System.Net.CookieContainer的CookieContainer =新System.Net.CookieContainer();
cookieContainer.Add(用户类型);
cookieContainer.Add(dUserType);HttpWebRequest的WebRequest的=(HttpWebRequest的)WebRequest.Create(URL);webRequest.Proxy =新WebProxy(127.0.0.1,8888);
webRequest.Referer =?的http://DOMAIN_NAME/search/index.aspx盖= 3;
webRequest.ContentType =应用/的X WWW的形式urlen codeD;
webRequest.Date = DateTime.Now;
webRequest.CookieContainer =的CookieContainer;字符串结果;使用(VAR流= webRequest.GetResponse()。GetResponseStream())
使用(VAR读者=新的StreamReader(流Encoding.UTF8))
{
    结果= reader.ReadToEnd();
}


解决方案

当您设置cookies,您还需要设置域,因为cookies是不同的 www.domain.com 和刚 domain.com

为了让他们一样,你需要设置在您的cookie。
首先在的web.config

 <身份验证模式=表格>
 <形式域=yoururl.com... />
< /认证>

和第二,当你手动设置你的cookies(用了WWW):

  Response.Cookies [cookieName]。域=yoururl.com;

或你设置它(与出子 WWW ):

  System.Net.Cookie dUserType =
    新System.Net.Cookie(dCUserType,用户,/,DOMAIN_NAME.com);

类似的问题和答案:结果
<一href=\"http://stackoverflow.com/questions/2454623/multiple-applications-using-same-login-database-logging-each-other-out/2491686#2491686\">Multiple使用相同的登录数据库记录相互应用出来结果
<一href=\"http://stackoverflow.com/questions/8236180/asp-net-forms-authentication-logged-out-when-logged-into-another-instance/8236300#8236300\">asp.net窗体身份验证注销时,登录到另一个实例结果
<一href=\"http://stackoverflow.com/questions/13940118/lost-session-cookie-when-login-as-another-user\">Lost会话/ cookie的登录时以其他用户

This is how I am setting up the cookies data and passing along web request. When I check the response from the website using fiddler I see cookies have no data and expiry data is set to Jan 1, 1900. Any help would be much appreciated. Please let me know if something is not clear from following code or you need more info to answer my question. Thanks.

System.Net.Cookie userType = 
    new System.Net.Cookie("CUserType","subscriber", "/", "www.DOMAIN_NAME.com");
userType.Expires = DateTime.Now.AddYears(1);
System.Net.Cookie dUserType = 
    new System.Net.Cookie("dCUserType", "subscriber", "/", "www.DOMAIN_NAME.com");
dUserType.Expires = DateTime.Now.AddYears(1);

System.Net.CookieContainer cookieContainer = new System.Net.CookieContainer();
cookieContainer.Add(userType);    
cookieContainer.Add(dUserType);

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URL);

webRequest.Proxy = new WebProxy("127.0.0.1", 8888);   
webRequest.Referer = "http://DOMAIN_NAME/search/index.aspx?lid=3";  
webRequest.ContentType = "application/x-www-form-urlencoded";   
webRequest.Date = DateTime.Now;    
webRequest.CookieContainer = cookieContainer;

string result;

using (var stream = webRequest.GetResponse().GetResponseStream())
using (var reader = new StreamReader(stream, Encoding.UTF8))    
{
    result = reader.ReadToEnd();    
}

解决方案

When you set cookies, you also need to set the domain because the cookies are different for www.domain.com and just domain.com.

To make them the same, you need to set the domain on your cookie. First on the web.config

<authentication mode="Forms">
 <forms domain="yoururl.com" ... />
</authentication> 

and second when you set manually your cookies (with out the www.):

Response.Cookies[cookieName].Domain = "yoururl.com";

or as you set it up (with out the subdomain www.):

System.Net.Cookie dUserType = 
    new System.Net.Cookie("dCUserType", "subscriber", "/", "DOMAIN_NAME.com");

Similar questions and answers :
Multiple applications using same login database logging each other out
asp.net forms authentication logged out when logged into another instance
Lost session/cookie when login as another user

这篇关于Cookies是不保留任何数据,同时作为的WebRequest的一部分发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆