Cookies参考在ashx文件中获取 [英] Cookies reference is getting in ashx file

查看:134
本文介绍了Cookies参考在ashx文件中获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了设置cookie并获取cookie


// set cookie
 HttpCookie cookie = new HttpCookie("uid");
                cookie.Value =user.ID;
                cookie.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(cookie);



//get cookie in ashx file

HttpCookie cookie = context.Request.Cookies.Get("uid");
string id = cookie.Value;


在上面的代码中,我首先设置了cookie,但是当我尝试在ashx文件中获取cookie值时(请参见下面的代码),则cookie对象为null,而这可以在aspx文件中轻松运行,而在ashx文件中为null. ="h2_lin">解决方案

如果在分配cookie值之前未调用ashx页面
那么你可以试试这个

 HttpCookie cookie =(HttpCookie)HttpContext.Current.Request.Cookies [" ];
字符串 id = cookie.Value; 


I have used set cookies and get cookies


// set cookie
 HttpCookie cookie = new HttpCookie("uid");
                cookie.Value =user.ID;
                cookie.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(cookie);



//get cookie in ashx file

HttpCookie cookie = context.Request.Cookies.Get("uid");
string id = cookie.Value;


In the above code first i have set cookies, but when i try to get cookies value in ashx file(see in the below code) then cookie object is null while this can run easily in aspx file but null in ashx file.

解决方案

If ashx page is not calling before the assign the cookie value
then you can try this

HttpCookie cookie =(HttpCookie)HttpContext.Current.Request.Cookies["uid"];
string id = cookie.Value;


这篇关于Cookies参考在ashx文件中获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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