如何使用 SSL 页面创建 Cookie?
我有一个基于 SSL 的 ASP:NET MVC 2 网站。我想创建一个像这样的cookie:
FormsAuthentication.SetAuthCookie(validatedUser.UserName, false);
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, validatedUser.SecureToken, DateTime.Now, DateTime.Now.AddMinutes(10), false, String.Empty);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
Response.Cookies.Add(cookie);
但我得到一个异常,告诉:“应用程序配置为发出安全cookie。这些cookie要求浏览器通过SSL(https协议)发出请求。但是,当前请求不是通过 SSL。”
在 web.config 中我有:
<authentication mode="Forms">
<forms loginUrl="~/Account/LoginError" timeout="2880" requireSSL="true" protection="All"/>
</authentication>
如何解决此问题?
I have an ASP:NET MVC 2 web site that is on SSL. I want to create a cookie like this:
FormsAuthentication.SetAuthCookie(validatedUser.UserName, false);
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, validatedUser.SecureToken, DateTime.Now, DateTime.Now.AddMinutes(10), false, String.Empty);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
Response.Cookies.Add(cookie);
But I get an exception, telling: "The application is configured to issue secure cookies. These cookies require the browser to issue the request over SSL (https protocol). However, the current request is not over SSL."
In web.config I have:
<authentication mode="Forms">
<forms loginUrl="~/Account/LoginError" timeout="2880" requireSSL="true" protection="All"/>
</authentication>
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
requireSSL="false"
或使用http://
请求您的网站。请注意,如果您关心安全性,那么这两种方法都不是好主意。如果您想要一个安全的网站,请保留requireSSL="true"
并使用https://
请求您的网站。此外 SetAuthCookie 方法已经写入cookie 到响应中,这样你就不需要剩下的了:
就足够了。您无需担心
FormsAuthenticationTicket
并将 cookie 添加到响应中。requireSSL="false"
or usehttp://
to request your site. Note that both are bad idea if you care about security. If you want a secure site leaverequireSSL="true"
and usehttps://
to request your site.Also the SetAuthCookie method already writes the cookie to the response so you don't need the rest:
is enough. You don't need to worry about
FormsAuthenticationTicket
and adding the cookie to the response.