安全类

  1//*********************************************************************
  2    //
  3    // TTSecurity Class
  4    //
  5    // The TimeTrackerSecurity class encapsulates two helper methods that enable
  6    // developers to easily check the role status of the current browser client.
  7    //
  8    //*********************************************************************
  9
 10    public class TTSecurity
 11    {                                                                                                                                
 12        //*********************************************************************
 13        //
 14        // TTSecurity.IsInRole() Method
 15        //
 16        // The IsInRole method enables developers to easily check the role
 17        // status of the current browser client.
 18        //
 19        //*********************************************************************
 20
 21        public static bool IsInRole(String role) 
 22        {
 23            return HttpContext.Current.User.IsInRole(role);
 24        }

 25
 26        //*********************************************************************
 27        //
 28        // TTSecurity.Encrypt() Method
 29        //
 30        // The Encrypt method encrypts a clean string into hashed string
 31        //
 32        //*********************************************************************
 33
 34        public static string Encrypt(string cleanString)
 35        {
 36            Byte[] clearBytes = new UnicodeEncoding().GetBytes(cleanString);
 37            Byte[] hashedBytes = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
 38            
 39            return BitConverter.ToString(hashedBytes);
 40        }

 41
 42        public static int GetUserID()
 43        {
 44            return ((CustomPrincipal)HttpContext.Current.User).UserID;
 45        }

 46
 47        public static string GetUserRole()
 48        {
 49            return ((CustomPrincipal)HttpContext.Current.User).UserRole;
 50        }

 51
 52        public static string GetName()
 53        {
 54            return ((CustomPrincipal)HttpContext.Current.User).Name;
 55        }

 56
 57                
 58        //*********************************************************************
 59        //
 60        // <summary>
 61        // Validates the input text using a Regular Expression and replaces any input expression
 62        // characters with empty string.Removes any characters not in [a-zA-Z0-9_]. 
 63        // <summary>
 64        // <remarks>
 65        // For a good reference on Regular Expressions, please see
 66        //     - http://regexlib.com
 67        //     - http://py-howto.sourceforge.net/regex/regex.html
 68        // </remarks>
 69        // <param name="inputText">The text to validate.</param>
 70        // <returns>Sanitized string</returns>
 71        //
 72        //*********************************************************************
 73
 74        public static string CleanStringRegex(string inputText)
 75        {
 76            RegexOptions options = RegexOptions.IgnoreCase;
 77            return ReplaceRegex(inputText,@"[^\\.!?""',\-\w\s@]",options);
 78        }

 79
 80        //*********************************************************************
 81        //
 82        // <summary>
 83        // Removes designated characters from an input string input text using a Regular Expression.
 84        // </summary>
 85        // <remarks>
 86        // For a good reference on Regular Expressions, please see
 87        //     - http://regexlib.com
 88        //     - http://py-howto.sourceforge.net/regex/regex.html
 89        // </remarks>
 90        // <param name="inputText">The text to clean.</param>
 91        // <param name="regularExpression">The regular expression</param>
 92        // <returns>Sanitized string.</returns>
 93        //
 94        //*********************************************************************
 95
 96        private static string ReplaceRegex(string inputText, string regularExpression, RegexOptions options)
 97        {
 98            Regex regex = new Regex(regularExpression,options);
 99            return regex.Replace(inputText,"");
100        }

101    }
posted @ 2006-12-27 17:37  tiasys  阅读(265)  评论(0编辑  收藏  举报