记录FormsAuthentication的理解与使用

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


配置mode=“Forms”
在这里插入图片描述

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

    //方法一
    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);
    }

退出登录,删除验证标签
:在这里插入图片描述
布局页的应用显示,是否登录不同标签显示的应用:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/MrLsss/article/details/106786425