2013-12-20 41 views
0

我有设置过的用户认证数据一个cookie:对象引用错误

HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket))); 

现在,当我尝试读取使用另一个类的饼干:

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返回相同的错误

EDIT2:好像HttpContext.Currentnull

回答

0

.NET 4.5有一些问题阅读HttpContext.Current

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

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