从外部类读取cookie时出现对象引用错误

时间:2013-12-20 07:29:47

标签: c# asp.net cookies

我有一个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中有数据。

谁能告诉我哪里出错了?我尝试了requestresponse

修改: HttpContext.Current.Request.Cookies.Get会返回相同的错误

编辑2:好像HttpContext.Current似乎是null

1 个答案:

答案 0 :(得分:0)

.net 4.5在阅读HttpContext.Current

时遇到了一些问题

Reading this answer,将以下内容添加到您的web.config

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>