记录FormsAuthentication的理解与使用

FormsAuthentication.SetAuthCookie() 就是产生客户端加密的Cookie,这样就代表验证通过,下一次访问需要验证的时候,服务器就读取Cookie,进行验证,不需要转到登录页面。
FormsAuthentication.RedirectFromLoginPage这个就是转到登录页面了(无登录)


配置mode=“Forms”
记录FormsAuthentication的理解与使用_第1张图片

登录:有两种方法,两者效果一致,后者可以带自定义数据

    //方法一
    FormsAuthentication.SetAuthCookie(customer.UserName, customer.IsRemember);

 
    //方法2,可以带自定义数据
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddDays(1), true, "自定义数据","/");
    string hashTicket = FormsAuthentication.Encrypt(ticket);
    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
    cookie.HttpOnly = true;
    Response.Cookies.Add(cookie);

    //重定向至原始请求URL上
    string returnUrl = FormsAuthentication.GetRedirectUrl(username, false);
    if (!string.IsNullOrEmpty(returnUrl))
    {
        Response.Redirect(returnUrl);
    }

退出登录,删除验证标签
:记录FormsAuthentication的理解与使用_第2张图片
布局页的应用显示,是否登录不同标签显示的应用:
记录FormsAuthentication的理解与使用_第3张图片

你可能感兴趣的:(Asp.Net,MVC)