1
我生成的Web API令牌设置后:如何读取angularjs一个cookie,它是使用的FormsAuthenticationTicket, 一样,在网络API
var user_json_str = new JavaScriptSerializer().Serialize(user);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
user.UserId,
DateTime.Now,
DateTime.Now.AddMinutes(30), false,
user_json_str, FormsAuthentication.FormsCookiePath);
string encTicket = FormsAuthentication.Encrypt(ticket);//ENCRYPT THE TICKET
在那之后,我尝试将其返回到用户在这样的响应标题:
HttpContext.Current.Response.Cookies.Add(new HttpCookie("encTicket", encTicket));
我在那之后发现了客户端的cookie。
我读的地方,它没有建议使用Web API这样的,所以我想是这样的:
string encTicket = FormsAuthentication.Encrypt(ticket);//ENCRYPT THE TICKET
HttpResponseMessage ans = new HttpResponseMessage();
ans = new HttpResponseMessage(HttpStatusCode.OK);
ans.Content = new StringContent("Logged");
ans.Headers.AddCookies(new[] { new CookieHeaderValue("encTicket", encTicket) });
return ans;
这一次的cookie并没有出现在客户端(我试图使用读它在Chrome控制台中的document.cookie)。
从web api的响应头中发送cookie的正确方法是什么?
见http://stackoverflow.com/questions/18513818/angularjs-cookie-read-response-value – PSL 2014-08-29 14:59:06
但如果我在控制台打印:document.cookie中它已经显示了所有提出的饼干页。我不认为$ cookie会让我看到其他隐藏的cookie。 – Lichte 2014-08-29 15:04:25
无论如何,我尝试了你的建议,正如我所想的那样,cookie是未定义的。我的问题是不读取cookie,但要确保它来到客户端 – Lichte 2014-08-29 15:17:44