【发布时间】:2010-11-09 12:40:06
【问题描述】:
基本上,我的网站中有一个要求,这意味着必须保护所有 cookie。我正在尝试保护 FormsAuthentication cookie,但是,我希望它在我的本地开发站点上我不必设置 SSL,但是实时站点仍将保护 cookie。
此实时/开发状态存储在配置 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);
所以我在“保存”部分收到错误消息。说有临时文件无法访问。
知道如何解决这个问题吗?
古普雷特
【问题讨论】:
-
最简单的答案是在生产中使用 IIS 规则保护您的站点,该规则将强制所有请求为 HTTPS,然后使用配置转换将表单身份验证配置的
requireSSL属性设置为 true。 -
嘿,克里斯,我将如何进行“使用配置转换将表单身份验证配置的 requireSSL 属性变为真”