.NET常用工具类(Utils.cs)

本文提供了一系列实用的字符串处理和验证方法,包括MD5和SHA256加密、电子邮件和URL的有效性验证、SQL安全检查及简繁体中文转换等。这些方法适用于多种应用场景,帮助开发者快速实现数据的安全性和准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


         /// <summary>
        
/// MD5函数
        
/// </summary>
        
/// <param name="str">原始字符串</param>
        
/// <returns>MD5结果</returns>

         public   static   string  MD5( string  str)
        
{
            
byte[] b = Encoding.Default.GetBytes(str);
            b 
= new MD5CryptoServiceProvider().ComputeHash(b);
            
string ret = "";
            
for(int i = 0; i < b.Length; i++)
                ret 
+= b[i].ToString("x").PadLeft(2,'0');
            
return ret;
        }

         /// <summary>
        
/// SHA256函数
        
/// </summary>
        
/// /// <param name="str">原始字符串</param>
        
/// <returns>SHA256结果</returns>

         public   static   string  SHA256( string  str)
        
{
            
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
            SHA256Managed Sha256 
= new SHA256Managed();
            
byte[] Result = Sha256.ComputeHash(SHA256Data);
            
return Convert.ToBase64String(Result);  //返回长度为44字节的字符串
        }

         /// <summary>
        
/// 检测是否符合email格式
        
/// </summary>
        
/// <param name="strEmail">要判断的email字符串</param>
        
/// <returns>判断结果</returns>

         public   static   bool  IsValidEmail( string  strEmail)
        
{
            
return Regex.IsMatch(strEmail, @"^([/w-/.]+)@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$");
        }


        
public   static   bool  IsValidDoEmail( string  strEmail)
        
{
            
return Regex.IsMatch(strEmail, @"^@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$");
        }

         /// <summary>
        
/// 检测是否是正确的Url
        
/// </summary>
        
/// <param name="strUrl">要验证的Url</param>
        
/// <returns>判断结果</returns>

         public   static   bool  IsURL( string  strUrl)
        
{
            
return Regex.IsMatch(strUrl, @"^(http|https)/://([a-zA-Z0-9/./-]+(/:[a-zA-Z0-9/.&%/$/-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9/-]+/.)*[a-zA-Z0-9/-]+/.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(/:[0-9]+)*(/($|[a-zA-Z0-9/./,/?/'///+&%/$#/=~_/-]+))*$");
        }


        
public   static   string  GetEmailHostName( string  strEmail)
        
{
            
if (strEmail.IndexOf("@"< 0)
            
{
                
return "";
            }

            
return strEmail.Substring(strEmail.LastIndexOf("@")).ToLower();
        }

         /// <summary>
        
/// 检测是否有Sql危险字符
        
/// </summary>
        
/// <param name="str">要判断字符串</param>
        
/// <returns>判断结果</returns>

         public   static   bool  IsSafeSqlString( string  str)
        
{

            
return !Regex.IsMatch(str, @"[-|;|,|//|/(|/)|/[|/]|/}|/{|%|@|/*|!|/']"); 
        }



         /// <summary>
        
/// 转换为简体中文
        
/// </summary>

         public   static   string  ToSChinese( string  str)
        
{
            
return Strings.StrConv(str, VbStrConv.SimplifiedChinese, 0) ;
        }


        
/// <summary>
        
/// 转换为繁体中文
        
/// </summary>

         public   static   string  ToTChinese( string  str)
        
{
            
return Strings.StrConv(str, VbStrConv.TraditionalChinese, 0);
        }


         /// <summary>
        
/// 返回 HTML 字符串的编码结果
        
/// </summary>
        
/// <param name="str">字符串</param>
        
/// <returns>编码结果</returns>

         public   static   string  HtmlEncode( string  str)
        
{
            
return HttpUtility.HtmlEncode(str);
        }


        
/// <summary>
        
/// 返回 HTML 字符串的解码结果
        
/// </summary>
        
/// <param name="str">字符串</param>
        
/// <returns>解码结果</returns>

         public   static   string  HtmlDecode( string  str)
        
{
            
return HttpUtility.HtmlDecode(str);
        }


        
/// <summary>
        
/// 返回 URL 字符串的编码结果
        
/// </summary>
        
/// <param name="str">字符串</param>
        
/// <returns>编码结果</returns>

         public   static   string  UrlEncode( string  str)
        
{
            
return HttpUtility.UrlEncode(str);
        }


        
/// <summary>
        
/// 返回 URL 字符串的编码结果
        
/// </summary>
        
/// <param name="str">字符串</param>
        
/// <returns>解码结果</returns>

         public   static   string  UrlDecode( string  str)
        
{
            
return HttpUtility.UrlDecode(str);
        }

         /// <summary>
        
/// 写cookie值
        
/// </summary>
        
/// <param name="strName">名称</param>
        
/// <param name="strValue"></param>
        
/// <param name="strValue">过期时间(分钟)</param>

         public   static   void  WriteCookie( string  strName,  string  strValue,  int  expires)
        
{
            HttpCookie cookie 
= HttpContext.Current.Request.Cookies[strName];
            
if (cookie == null)
            
{
                cookie 
= new HttpCookie(strName);
            }

            cookie.Value 
= strValue;
            cookie.Expires 
= DateTime.Now.AddMinutes(expires);
            HttpContext.Current.Response.AppendCookie(cookie);

        }


        
/// <summary>
        
/// 读cookie值
        
/// </summary>
        
/// <param name="strName">名称</param>
        
/// <returns>cookie值</returns>

         public   static   string  GetCookie( string  strName)
        
{
            
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
            
{
                
return HttpContext.Current.Request.Cookies[strName].Value.ToString();
            }


            
return "";
        }


 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值