基本上,我的网站中有一项要求,即所有Cookie都必须安全。我试图确保FormsAuthentication cookie的安全,但是,我希望在我的本地开发站点上不需要设置SSL,但活动站点仍然可以保护cookie。启用实时环境的Cookie和启用环境的非SSL的SSL
此live/dev状态存储在配置xml文件中。该文件包含网站运行的每台机器的设置。它可以通过Config.IsSecure
if (Config.IsSecure)
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, login.Username, DateTime.Now, DateTime.Now.AddMinutes(30), false, "User", FormsAuthentication.FormsCookiePath);
string cookieStr = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieStr);
cookie.Path = FormsAuthentication.FormsCookiePath;
System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
AuthenticationSection authenticationSection = (AuthenticationSection)configuration.GetSection("system.web/authentication");
FormsAuthenticationConfiguration formsAuthentication = authenticationSection.Forms;
formsAuthentication.RequireSSL = true;
cookie.Secure = true;
configuration.Save()
}
FormsAuthentication.SetAuthCookie(login.Username, false);
所以我得到一个错误的保存部分。说有一个临时文件无法访问。
任何想法如何解决这个问题?
Gurpreet
最简单的答案是在生产中使用IIS规则来保护您的站点,该规则将强制所有请求为HTTPS,然后使用配置转换将表单验证配置的'requireSSL'属性设置为true。 – 2010-11-11 14:28:05
嘿克里斯,我将如何去“使用配置转换来将表单验证配置的requireSSL属性为真” – swissarmykirpan 2010-12-13 17:28:04