基本上,我的網站中有一項要求,即所有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