AdolphYang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

常用函数:

CommonHelper.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using RazorEngine;
using RazorEngine.Text;

namespace DIDAO.Common
{
    public class CommonHelper
    {
        /// <summary>
        /// 传入虚拟路径 返回全路径的html字符串
        /// </summary>
        /// <param name="context">当前数据上下文</param>
        /// <param name="virtualPath">虚拟路径</param>
        /// <returns>返回全路径的html字符串</returns>
        public static string GetHtmlFromVirtualPath(HttpContext context,string virtualPath)
        {
            string fileFullPath = context.Server.MapPath(virtualPath);
            string html = File.ReadAllText(fileFullPath);
            return html;
        }

        /// <summary>
        /// 输出错误信息到错误界面
        /// </summary>
        /// <param name="context">当前数据上下文</param>
        /// <param name="virtualPath">错误页面的 虚拟路径</param>
        /// <param name="errormsg">错误信息</param>
        public static void OutputError(HttpContext context, string virtualPath,string errormsg)
        {
            string htmlError = GetHtmlFromVirtualPath(context, virtualPath);
            string htmlNewError = htmlError.Replace("{errormsg}", errormsg);
            context.Response.Write(htmlNewError);
        }
        
        /// <summary>
         /// 加密
         /// </summary>
         /// <param name="str">原始密码</param>
         /// <returns>加密密码</returns>
         public static string Md5Encode(string str)
         {
             byte[] bytes = System.Text.Encoding.Default.GetBytes(str);
             MD5 md5 = new MD5CryptoServiceProvider();
             byte[] newBytes = md5.ComputeHash(bytes);
             string newStr = BitConverter.ToString(newBytes).Replace("-","");
             return newStr;
         }

         /// <summary>
         /// 检查字符串 是否含有特殊字符 :含有-false
         /// </summary>
         /// <param name="str">输入字符串</param>
         /// <returns>是否含有特殊字符:含有-false</returns>
         public static bool CheckStringIsSpecialChar(string str)
         {
             bool flag = true;
             char[] chs = { ',', '/', '\\', '\'', '"' };
             foreach (char ch in chs)
             {
                 if (str.IndexOf(ch) >= 0)
                 {
                     flag = false;
                 }
             }
             return flag;
         }


        //DES NET加密解密
         public static string _KEY = "YANGGUO1";  //密钥
         public static string _IV = "YANGGUO2";   //向量

         /// <summary>
         /// 加密
         /// </summary>
         /// <param name="data"></param>
         /// <returns></returns>
         public static string DesEncode(string data)
         {

             byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(_KEY);
             byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(_IV);

             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
             int i = cryptoProvider.KeySize;
             MemoryStream ms = new MemoryStream();
             CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

             StreamWriter sw = new StreamWriter(cst);
             sw.Write(data);
             sw.Flush();
             cst.FlushFinalBlock();
             sw.Flush();

             string strRet = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
             return strRet;
         }

         /// <summary>
         /// 解密
         /// </summary>
         /// <param name="data"></param>
         /// <returns></returns>
         public static string DesDecode(string data)
         {

             byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(_KEY);
             byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(_IV);

             byte[] byEnc;

             try
             {
                 data.Replace("_%_", "/");
                 data.Replace("-%-", "#");
                 byEnc = Convert.FromBase64String(data);

             }
             catch
             {
                 return null;
             }

             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
             MemoryStream ms = new MemoryStream(byEnc);
             CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
             StreamReader sr = new StreamReader(cst);
             return sr.ReadToEnd();
         }

        

         
    }
}
CommonHelper.cs

 

有关Razor引擎进行cshtml页面解析的函数:

RazorHelper.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using RazorEngine;
using RazorEngine.Text;

namespace DIDAO.Common
{
    public class RazorHelper
    {
        /// <summary>
        /// Razor解析cshtml页面,并输出到浏览器
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="cshtmlVirtualPath">cshtml页面的虚拟路径</param>
        /// <param name="model">传递的虚拟实例,可不传</param>
        public static void RazorParse(HttpContext context, string cshtmlVirtualPath, object model=null)
        {
            string fullPath = context.Server.MapPath(cshtmlVirtualPath);
            string cshtml = File.ReadAllText(fullPath);
            string cacheName = fullPath + File.GetLastWriteTime(fullPath);
            string html = Razor.Parse(cshtml, model, cacheName);
            context.Response.Write(html);
        }

        /// <summary>
        /// 对html进行加密
        /// </summary>
        /// <param name="htmlStr">html标签</param>
        /// <returns>加密之后的字符串</returns>
        public static HtmlEncodedString HtmlEncodedString(string htmlStr)
        {
            return new HtmlEncodedString(htmlStr);
        }

        /// <summary>
        /// 对html原样显示
        /// </summary>
        /// <param name="htmlStr">html标签</param>
        /// <returns>html原来样子</returns>
        public static RawString RawString(string htmlStr)
        {
            return new RawString(htmlStr);
        }

        /// <summary>
        /// 拼接生成CheckBox 标签
        /// </summary>
        /// <param name="isCheck">是否选中</param>
        /// <param name="extendProperties">扩展属性的对象:比如,new {id='managerId',name='manager',style='color:red' }</param>
        /// <returns>CheckBox标签</returns>
        public static RawString CheckBox(bool isCheck, object extendProperties)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<input type='checkbox' ");
            sb.Append(RenderExtProperties(extendProperties));
            if (isCheck)
            {
                sb.Append(" checked ");
            }
            sb.AppendLine(" />");
            return new RawString(sb.ToString());
        }

        /// <summary>
        /// 拼接扩展属性 及对应的值
        /// </summary>
        /// <param name="extendProperties">扩展属性 所在的匿名实例</param>
        /// <returns>拼接生成的 包含属性名和值 的字符串: 比如,“ name='manager' id='managerId' ” </returns>
        private static string RenderExtProperties(object extendProperties)
        {
            StringBuilder sb = new StringBuilder();
            #region 拼接扩展属性
            Type extType = extendProperties.GetType();
            PropertyInfo[] props = extType.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                string extPropName = prop.Name;
                object extPropValue = prop.GetValue(extendProperties);
                sb.Append(" ").Append(extPropName).Append("='").Append(extPropValue).Append("' ");
            }
            #endregion
            return sb.ToString();
        }

        /// <summary>
        /// 拼接生成DropDownList下拉列表 标签
        /// </summary>
        /// <param name="list">实例的集合</param>
        /// <param name="valuePropName">实际的值属性的名称:比如,Id</param>
        /// <param name="textPropName">显示的文本属性的名称:比如,Name</param>
        /// <param name="selectedValue">选中的值</param>
        /// <param name="extendProperties">扩展属性的对象:比如,new {id='managerId',name='manager',style='color:red' }</param>
        /// <returns>DropDownList下拉列表 标签</returns>
        public static RawString DropDownList(IEnumerable list, string valuePropName, string textPropName, object selectedValue, object extendProperties)
        {
            //<select name='' id='' >
            //<option value=''> </option> 
            //</select>
            StringBuilder sb = new StringBuilder();
            sb.Append("<select ");
            #region 拼接扩展属性
            sb.Append(RenderExtProperties(extendProperties));
            #endregion
            sb.AppendLine(" >");
            #region 拼接下拉选项
            foreach (object item in list)
            {
                object valuePropValue, textPropValue;
                GetvalueAndTextPropValue(item, valuePropName, textPropName, out valuePropValue, out textPropValue);
                sb.Append("<option value='").Append(valuePropValue).Append("' ");
                if (object.Equals(valuePropValue, selectedValue)) //如果当前值与选中的值相等,则selected (引用类型用equal,如果用=则是不同的实例,因为发生过装箱)
                {
                    sb.Append(" selected ");
                }
                sb.Append(">").Append(textPropValue).AppendLine(" </option> ");
            }
            #endregion
            sb.AppendLine("</select>");
            return new RawString(sb.ToString());
        }

        /// <summary>
        /// 拼接生成RadioButtonList 标签
        /// </summary>
        /// <param name="list">实例的集合</param>
        /// <param name="valuePropName">实际的值属性的名称:比如,Id</param>
        /// <param name="textPropName">显示的文本属性的名称:比如,Name</param>
        /// <param name="selectedValue">选中的值</param>
        // <param name="extendProperties">扩展属性的对象:比如,new {name='gender',style='color:red' }</param>
        /// <returns>RadioButtonList 标签</returns>
        public static RawString RadioButtonList(IEnumerable list, string valuePropName, string textPropName, object selectedValue, object extendProperties)
        {
            //<input type="radio" name="gender" value="1" checked /><label>男</label><br />  //只能单选
            StringBuilder sb = new StringBuilder();
            foreach (object item in list)
            {
                object valuePropValue, textPropValue;
                GetvalueAndTextPropValue(item, valuePropName, textPropName, out valuePropValue, out textPropValue);
                sb.Append("<input type=\"radio\" ");
                sb.Append(RenderExtProperties(extendProperties));
                sb.Append(" value=\"").Append(valuePropValue).Append("\"");
                if (object.Equals(valuePropValue, selectedValue))
                {
                    sb.Append(" checked ");
                }
                sb.Append(" /><label>").Append(textPropValue).AppendLine("</label><br />");
            }
            return new RawString(sb.ToString());
        }

        /// <summary>
        /// 拼接生成CheckBoxList 标签
        /// </summary>
        /// <param name="list">实例的集合</param>
        /// <param name="valuePropName">实际的值属性的名称:比如,Id</param>
        /// <param name="textPropName">显示的文本属性的名称:比如,Name</param>
        /// <param name="selectedValues">选中的值的数组</param>
        /// <param name="extendProperties">扩展属性的对象:比如,new {name='hobby',style='color:red' }</param>
        /// <returns>CheckBoxList 标签</returns>
        public static RawString CheckBoxList(IEnumerable list, string valuePropName, string textPropName, object[] selectedValues, object extendProperties)
        {
            //<input type="checkbox" name="hobby" value="1" checked /><label>足球</label><br />   //可多选
            StringBuilder sb = new StringBuilder();
            foreach (object item in list)
            {
                object valuePropValue, textPropValue;
                GetvalueAndTextPropValue(item, valuePropName, textPropName, out valuePropValue, out textPropValue);
                sb.Append("<input type=\"checkbox\" ");
                sb.Append(RenderExtProperties(extendProperties));
                sb.Append(" value=\"").Append(valuePropValue).Append("\" ");
                if (selectedValues.Contains(valuePropValue))
                {
                    sb.Append(" checked ");
                }
                sb.Append(" /><label>").Append(textPropValue).AppendLine("</label><br />");
            }
            return new RawString(sb.ToString());
        }

        /// <summary>
        /// 根据指定实例的 值属性名和文本属性名 获得 值属性值和文本属性值
        /// </summary>
        /// <param name="item">指定实例</param>
        /// <param name="valuePropName">值属性名</param>
        /// <param name="textPropName">文本属性名</param>
        /// <param name="valuePropValue">out 值属性值</param>
        /// <param name="textPropValue">out 文本属性值</param>
        private static void GetvalueAndTextPropValue(object item, string valuePropName, string textPropName, out object valuePropValue, out object textPropValue)
        {
            Type type = item.GetType();
            PropertyInfo valueProp = type.GetProperty(valuePropName);
            valuePropValue = valueProp.GetValue(item);
            PropertyInfo textProp = type.GetProperty(textPropName);
            textPropValue = textProp.GetValue(item);
        }
    }
}
RazorHelper.cs

 

有关Ajax异步请求的函数:

AjaxHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Script.Serialization;

namespace DIDAO.Common
{
    public class AjaxHelper
    {
        /// <summary>
        /// Ajax返回json数据
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="status">状态码:ok,error</param>
        /// <param name="msg">信息</param>
        /// <param name="data">数据:默认null</param>
        public static void WriteJson(HttpContext context,string status,string msg,object data=null)
        {
            string json = new JavaScriptSerializer().Serialize(new { status = status, msg = msg, data = data });
            context.Response.Write(json);
        }
    }
}
AjaxHelper.cs

 

有关项目中变量类型的转换和验证:

VolidHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Script.Serialization;

namespace DIDAO.Common
{
    public class AjaxHelper
    {
        /// <summary>
        /// Ajax返回json数据
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="status">状态码:ok,error</param>
        /// <param name="msg">信息</param>
        /// <param name="data">数据:默认null</param>
        public static void WriteJson(HttpContext context,string status,string msg,object data=null)
        {
            string json = new JavaScriptSerializer().Serialize(new { status = status, msg = msg, data = data });
            context.Response.Write(json);
        }
    }
}
VolidHelper.cs

 

专门用于存储 常量/枚举 字符串的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DIDAO.Common
{
    /// <summary>
    /// 存储常量/枚举 字符串 
    /// </summary>
    public class ConstStringHelper
    {
        /// <summary>
        /// admin session中的验证码
        /// </summary>
        public const string ADMINSESSION_VALIDCODE = "AdminSession_ValidCode";
        /// <summary>
        /// admin cookie中是否记忆密码
        /// </summary>
        public const string ADMINCOOKIE_CHKMEMORYPWD = "AdminCookie_chkMemoryPwd";
        /// <summary>
        /// admin cookie中是否自动登录
        /// </summary>
        public const string ADMINCOOKIE_CHKAUTOLOGIN = "AdminCookie_chkAutoLogin";
        /// <summary>
        /// admin cookie中用户名
        /// </summary>
        public const string ADMINCOOKIE_USERNAME = "AdminCookie_UserName";
        /// <summary>
        /// admin cookie中密码
        /// </summary>
        public const string ADMINCOOKIE_PASSWORD = "AdminCookie_Password";
        /// <summary>
        /// admin session中ID
        /// </summary>
        public const string ADMINSESSION_ID = "AdminSession_Id";
        /// <summary>
        /// admin session中用户名
        /// </summary>
        public const string ADMINSESSION_USERNAME = "AdminSession_UserName";


        /// <summary>
        /// 枚举 登录状态
        /// </summary>
        public enum LoginResult { 
            OK, //登录成功
            UserNameNotExist, //用户名不存在
            PasswordError  //密码错误
        };

        /// <summary>
        /// 枚举 自动登录状态
        /// </summary>
        public enum AutoLoginResult { 
            AutoLogin, //自动登录成功
            MemoryPwd, //填充密码
            NO  //自动登录失败
        };
    }
}
ConstStringHelper.cs

 

用于当前项目的类,实际起到BLL的作用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using DIDAO.BLL;
using DIDAO.Common;
using DIDAO.Model;

namespace DIDAO.Admin.Common
{
    public class LoginHelper
    {
        /// <summary>
        /// 判断验证码是否正确
        /// </summary>
        /// <param name="context"></param>
        /// <param name="validCode">用户输入的验证码</param>
        /// <returns>验证码是否正确</returns>
        public static bool CheckValidCode(HttpContext context, string validCode)
        {
            string sessionValidCode = (string)context.Session[ConstStringHelper.ADMINSESSION_VALIDCODE];
            return sessionValidCode == validCode;
        }

        /// <summary>
        /// 把用户名和密码 存入cookie
        /// </summary>
        /// <param name="context"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public static void StoreCookie(HttpContext context, string chkMemoryPwd, string chkAutoLogin, string username, string password)
        {
            context.Response.SetCookie(new HttpCookie(ConstStringHelper.ADMINCOOKIE_CHKMEMORYPWD) { Value = chkMemoryPwd, Expires = DateTime.Now.AddDays(7) });
            context.Response.SetCookie(new HttpCookie(ConstStringHelper.ADMINCOOKIE_CHKAUTOLOGIN) { Value = chkAutoLogin, Expires = DateTime.Now.AddDays(7) });
            context.Response.SetCookie(new HttpCookie(ConstStringHelper.ADMINCOOKIE_USERNAME) { Value = username, Expires = DateTime.Now.AddDays(7) });
            context.Response.SetCookie(new HttpCookie(ConstStringHelper.ADMINCOOKIE_PASSWORD) { Value = CommonHelper.DesEncode(password), Expires = DateTime.Now.AddDays(7) });
        }

        /// <summary>
        /// 把用户id和用户名 存入session 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="id"></param>
        /// <param name="username"></param>
        public static void StoreSession(HttpContext context,long id,string username)
        {
            context.Session[ConstStringHelper.ADMINSESSION_ID] = id;
            context.Session[ConstStringHelper.ADMINSESSION_USERNAME] = username;
        }

        /// <summary>
        /// 尝试自动登录 或填充密码
        /// </summary>
        /// <param name="context"></param>
        /// <returns>自动登录后的状态:成功,填充密码,失败继续</returns>
        public static ConstStringHelper.AutoLoginResult TryAutoLoginOrMemoryPwd(HttpContext context,out string username,out string password)
        {
            username = ""; password = "";
            //尝试自动登录
            HttpCookie cookie_chkAutoLogin = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_CHKAUTOLOGIN];
            if (cookie_chkAutoLogin!=null && cookie_chkAutoLogin.Value=="true") 
            {
                HttpCookie cookie_username = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_USERNAME];
                HttpCookie cookie_password = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_PASSWORD];
                if (cookie_username != null && cookie_password!=null)
                {
                    if (CheckLoginStatus(context, cookie_username.Value, CommonHelper.DesDecode(cookie_password.Value)) == ConstStringHelper.LoginResult.OK)
                    {
                        return ConstStringHelper.AutoLoginResult.AutoLogin;
                    }
                }
            }
            //尝试填充密码
            HttpCookie cookie_chkMemoryPwd = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_CHKMEMORYPWD];
            if (cookie_chkMemoryPwd != null && cookie_chkMemoryPwd.Value == "true")
            {
                HttpCookie cookie_username = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_USERNAME];
                HttpCookie cookie_password = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_PASSWORD];
                if (cookie_username != null && cookie_password != null)
                {
                    username = cookie_username.Value;
                    password = CommonHelper.DesDecode(cookie_password.Value);
                    return ConstStringHelper.AutoLoginResult.MemoryPwd;
                }
            }
            return ConstStringHelper.AutoLoginResult.NO;
        }

        /// <summary>
        /// 检查登录状态: ok 成功,UserNameNotExist 用户名不存在,PasswordError 密码错误
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns>枚举: ok 成功,UserNameNotExist 用户名不存在,PasswordError 密码错误</returns>
        public static ConstStringHelper.LoginResult CheckLoginStatus(HttpContext context,string username,string password)
        {
            List<object> list = new MyORM_BLL().SelectModelByField(typeof(TD_ADMIN), "USERNAME='" + username + "'"); //需要先检验username是否有特殊字符
            if (list.Count<=0)
            {
                return ConstStringHelper.LoginResult.UserNameNotExist;
            }
            else if (list.Count == 1)
            {
                TD_ADMIN admin = list[0] as TD_ADMIN;
                if (admin.PASSWORD != CommonHelper.Md5Encode(password))
                {
                    return ConstStringHelper.LoginResult.PasswordError;
                }
                else
                {
                    StoreSession(context, admin.ID, username); //存入session
                    return ConstStringHelper.LoginResult.OK;
                }
            }
            else
            {
                throw new Exception("数据库错误:用户名重复:" + username);
            }
        }
    }
}
LoginHelper.cs

 

posted on 2015-09-17 19:30  AdolphYang  阅读(259)  评论(0编辑  收藏  举报