Form authentication(表单认证)问题

前言

最近在做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

Configure MachineKey

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值