我有一个cookie,其中包含用户身份验证数据集:
HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)));
现在,当我尝试使用以下方法从另一个类读取cookie时
HttpCookie myCookie = new HttpCookie(FormsAuthentication.FormsCookieName);
myCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
我收到Object reference not set to an instance of an object
错误
我已经检查了我的浏览器cookie,.ASPXAUTH
cookie中有数据。
谁能告诉我哪里出错了?我尝试了request
和response
修改: HttpContext.Current.Request.Cookies.Get
会返回相同的错误
编辑2:好像HttpContext.Current
似乎是null
答案 0 :(得分:0)
.net 4.5在阅读HttpContext.Current
Reading this answer,将以下内容添加到您的web.config
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>