コード例 #1
0
 /// <summary>
 /// Returns an instance of a CustomIdentity class, 
 /// given an encrypted authentication string obtained from an HTTP cookie.
 /// </summary>
 /// <param name="encryptedInput">Encrypted string conataining User Identity</param>
 /// <returns>CustomIdentity object</returns>
 public static CustomIdentity Decrypt(string encryptedInput)
 {
     CustomIdentity identity = null;
     try
     {
         string decryptedString = CustomEncryption.Decrypt(encryptedInput);
         //string[] userProperties = decryptedString.Split(new char[] {'|'});
         ArrayList userProperties = new ArrayList();
         Split("|$|", decryptedString, userProperties);
         if(userProperties != null && userProperties.Count > 0)
         {
             identity = new CustomIdentity();
             Type t_identity = identity.GetType();
             PropertyInfo[] propertyInfo = t_identity.GetProperties();
             for( int i = 0; i < propertyInfo.Length; i++)
             {
                 PropertyInfo property = propertyInfo[i];
                 if(property.CanWrite)
                 {
                     string propertyValue = userProperties[i].ToString();
                     object objValue = Convert.ChangeType(propertyValue, property.PropertyType);
                     property.SetValue(identity, objValue, null);
                 }
             }
         }
     }
     catch(Exception e)
     {
         string str = e.Message;
         throw ;
     }
     return identity;
 }
コード例 #2
0
        /// <summary>
        /// Produces a string containing an encrypted string for an authenticated User Identity
        /// suitable for use in an HTTP cookie given a CustomIdentity
        /// </summary>
        /// <param name="identity">CustomIdentity class for the authenticated user</param>
        /// <returns>Encrypted string</returns>
        public static string Encrypt(CustomIdentity identity)
        {
            string encryptedString = String.Empty;
            try
            {
                StringBuilder en_str = new StringBuilder();
                Type t_Identity = identity.GetType();
                PropertyInfo[] propertyInfo = t_Identity.GetProperties();
                foreach(PropertyInfo property in propertyInfo)
                {
                    en_str.Append(property.GetValue(identity, null));
                    en_str.Append("|$|");
                }

                encryptedString = CustomEncryption.Encrypt(en_str.ToString());
            }
            catch(Exception e)
            {
                string str = e.Message;
                throw ;
            }
            return encryptedString;
        }