System.Web.Security.FormsAuthentication.Decrypt C# (CSharp) Method

Decrypt() public static method

public static Decrypt ( string encryptedTicket ) : System.Web.Security.FormsAuthenticationTicket
encryptedTicket string
return System.Web.Security.FormsAuthenticationTicket
		public static FormsAuthenticationTicket Decrypt (string encryptedTicket)
		{
			if (String.IsNullOrEmpty (encryptedTicket))
				throw new ArgumentException ("Invalid encrypted ticket", "encryptedTicket");

			Initialize ();

			FormsAuthenticationTicket ticket;
			byte [] bytes = Convert.FromBase64String (encryptedTicket);

			try {
				ticket = Decrypt2 (bytes);
			} catch (Exception) {
				ticket = null;
			}

			return ticket;
		}

Usage Example

Example #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 1. 读登录Cookie
                HttpCookie cookie = Request.Cookies[FA.FormsCookieName];
                if (cookie == null || string.IsNullOrEmpty(cookie.Value))
                {
                    return;
                }

                try
                {
                    string userData = null;
                    // 2. 解密Cookie值,获取FormsAuthenticationTicket对象
                    FormsAuthenticationTicket ticket = FA.Decrypt(cookie.Value);

                    if (ticket != null && string.IsNullOrEmpty(ticket.UserData) == false)
                    {
                        // 3. 还原用户数据
                        userData = ticket.UserData;
                    }

                    //反序列化对象

                    Context.User = null;
                }
                catch { /* 有异常也不要抛出,防止攻击者试探。 */ }
            }
        }
All Usage Examples Of System.Web.Security.FormsAuthentication::Decrypt