mvc 使用authentication 进行验证时,链接自动跳转。

来源:互联网 发布:网络借贷行业准入标准 编辑:程序博客网 时间:2024/06/01 09:48

 

1.

使用form窗体验证,(第三种方式)

////创建身份验证票
2   FormsAuthenticationTicket AuTicket =new FormsAuthenticationTicket(
3 1, UserInfo.UserName, DateTime.Now, DateTime.Now.AddMinutes(30),
4 false, Request.UserHostAddress);
5 ////将票据加密
6  string authTicket = FormsAuthentication.Encrypt(AuTicket);
7 ////将加密后的票据保存为cookie 
8 HttpCookie coo =new HttpCookie(FormsAuthentication.FormsCookieName, authTicket);
9 coo.Secure =false;
10 coo.Expires = AuTicket.Expiration;
11 coo.Path = FormsAuthentication.FormsCookiePath;
12 ////加入新cookie 
13 Response.Cookies.Add(coo);

 

问题,当在遨游浏览器浏览页面时,自动跳到登陆页面。(前提我已经登陆了。)

解决方法:在web.config配置文件中添加以下节点,注意:cookieless=“UseCookies” 是必需的。

 

 <authentication mode="Forms">

      <forms loginUrl="~/Account/Index" timeout="2880" cookieless="UseCookies" />        

    </authentication>

原创粉丝点击