最近在做ASP.NET MVC中表單認證時出了一些問題,特此記錄。
進行表單認證時,在 PostAuthenticateRequest 事件中從Cookie值中解密票據。如下:
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
TODO......
}
}
昨天一點問題都沒有,今天再次運行卻意外出現如下結果:
此問題從未遇見過,只能求助於stackoverflow了。
最終發現需要在配置文件中添加認證方式:
<authentication mode="Forms">
</authentication>
如上設置即可解決問題。
上述加密和解密 FormsAuthentication.Encrypt and Decrypt 也和 machine key 有關,如上述未解決可通過設置machine key來解決。
參考網址:
formsauthentication unable to validate data
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。