简体   繁体   English

cookie第一次显示空值,再次到达该页面后,我可以看到cookie值。 Asp.net c#

[英]cookie is showing null value for first time and after coming again to that page, i can see cookie value. Asp.net c#

I am developing a website in which user has to buy a subscription, what i am trying to do is that when user log in to my website for buying subscription i save his/her email id in a cookie and then he/she is redirected to paypal and after paying money he comes back to success page of my website and there i retrieves the cookie of his email id to update database but there for first time i gets null value and if i repeat the same process again i can read the value in cookie which i stored. 我正在开发一个用户必须购买订阅的网站,我想要做的是,当用户登录我的网站购买订阅时,我将他/她的电子邮件ID保存在cookie中然后他/她被重定向到paypal和付钱后,他回到我的网站的成功页面,我在那里检索他的电子邮件ID的cookie来更新数据库但是第一次我获得空值,如果我再次重复相同的过程,我可以读取值我存储的cookie。 On my localhost this problem wasn't coming but when i published the website this problem came. 在我的本地主机上这个问题没有出现,但是当我发布网站时出现了这个问题。 help me out The code i am using to store the cookie 帮帮我我用来存储cookie的代码

Response.Cookies["useremail"].Value = Session["user_id"].ToString();
Response.Cookies["useremail"].Expires = DateTime.Now.AddDays(1);

The code i am using to retrieve the cookie 我用来检索cookie的代码

if (Request.Cookies["useremail"] != null)
      Label1.Text= Server.HtmlEncode(Request.Cookies["useremail"].Value);

On the browser i have seen that cookie is saved. 在浏览器上,我看到cookie已保存。

In a nutshell this is what I would expect to see in your code but I do not so you can try this and replace it with the proper variables etc to make your code work. 简而言之,这是我期望在您的代码中看到的,但我不这样做,您可以尝试使用适当的变量等替换它,以使您的代码工作。 I test this on my test server and it works so you can follow the same pattern. 我在我的测试服务器上测试它,它可以工作,所以你可以遵循相同的模式。

string myCookieName = "abcd";
HttpCookie aCookie = new HttpCookie(myCookieName);
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM