Examples of FormsAuthentication usage in C

  • 2020-12-22 17:45:13
  • OfStack


using System;
using System.Web;
using System.Web.Security;

namespace AuthTest
{
  public class Authentication
  {
    /// <summary>
    ///  Set user login successful credentials ( Cookie Storage) 
    /// </summary>
    /// <param name="UserName"> The user name </param>
    /// <param name="PassWord"> password </param>
    /// <param name="Rights"> permissions </param>
    public static void SetCookie(string UserName,string PassWord,string Rights)
    {
      //
      //String PassWord="test";
      //
      String UserData = UserName + "#" + PassWord+"#"+Rights;
      if (true)
      {
        // Data into the ticket
        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(60), false, UserData);
        // Data encryption 
        string enyTicket = FormsAuthentication.Encrypt(ticket);
        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, enyTicket);
        HttpContext.Current.Response.Cookies.Add(cookie);
      }
    }
    /// <summary>
    ///  Determine if the user is logged in 
    /// </summary>
    /// <returns>True,Fales</returns>
    public static bool isLogin()
    {
      return HttpContext.Current.User.Identity.IsAuthenticated;
    }
    /// <summary>
    ///  Cancellation of landing 
    /// </summary>
    public static void logOut()
    {
      FormsAuthentication.SignOut();
    }
    /// <summary>
    ///  Gets the user name in the credentials 
    /// </summary>
    /// <returns> The user name </returns>
    public static string getUserName()
    {
      if (isLogin())
      {
        string strUserData = ((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;
        string[] UserData = strUserData.Split('#');
        if (UserData.Length != 0)
        {
          return UserData[0].ToString();
        }
        else
        {
          return "";
        }
      }
      else
      {
        return "";
      }
    }
    /// <summary>
    ///  Gets the password in the credential 
    /// </summary>
    /// <returns> password </returns>
    public static string getPassWord()
    {
      if (isLogin())
      {
        string strUserData = ((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;
        string[] UserData = strUserData.Split('#');
        if (UserData.Length!=0)
        {
          return UserData[1].ToString();
        }
        else
        {
          return "";
        }
      }
      else
      {
        return "";
      }
    }
    /// <summary>
    ///  Gets user permissions in credentials 
    /// </summary>
    /// <returns> User permissions </returns>
    public static string getRights()
    {
      if (isLogin())
      {
        string strUserData = ((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;
        string[] UserData = strUserData.Split('#');
        if (UserData.Length!=0)
        {
          return UserData[2].ToString();
        }
        else
        {
          return "";
        }
      }
      else
      {
        return "";
      }
    }
  }
}


Related articles: