DIDAO.Common --- 项目中的常用类及其中函数
常用函数:
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) >= )
{
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(), , (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() });
context.Response.SetCookie(new HttpCookie(ConstStringHelper.ADMINCOOKIE_CHKAUTOLOGIN) { Value = chkAutoLogin, Expires = DateTime.Now.AddDays() });
context.Response.SetCookie(new HttpCookie(ConstStringHelper.ADMINCOOKIE_USERNAME) { Value = username, Expires = DateTime.Now.AddDays() });
context.Response.SetCookie(new HttpCookie(ConstStringHelper.ADMINCOOKIE_PASSWORD) { Value = CommonHelper.DesEncode(password), Expires = DateTime.Now.AddDays() });
} /// <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<=)
{
return ConstStringHelper.LoginResult.UserNameNotExist;
}
else if (list.Count == )
{
TD_ADMIN admin = list[] 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
DIDAO.Common --- 项目中的常用类及其中函数的更多相关文章
- Qt 中一些常用类中文说明
Qt 中一些常用类中文说明是本文讲述的内容,这篇文章主要是介绍Qt 当中经常使用的类,采取的是使用字母索引的方式,下面的类是被经常使用的. QDataStream 为QIODevice提供了一串的二进 ...
- java-API中的常用类,新特性之-泛型,高级For循环,可变参数
API中的常用类 System类System类包含一些有用的类字段和方法.它不能被实例化.属性和方法都是静态的. out,标准输出,默认打印在控制台上.通过和PrintStream打印流中的方法组合构 ...
- javaAPI中的常用 类 以及接口
java.lang包中的常用类以及接口 类 1. Integer :Integer 类在对象中包装了一个基本类型 int 的值.Integer 类型的对象包含一个 int 类型的字段. 2. Math ...
- java中的常用类(二)
java中的常用类(二) Math类 Math类的声明:public final class Math extends Object Math类是与数学计算有关的类,里面的方法都是静态方法,直接使用类 ...
- java 中的常用类
Java 中的包装类 相信各位小伙伴们对基本数据类型都非常熟悉,例如 int.float.double.boolean.char 等. 基本数据类型是不具备对象的特性的,比如基本类型不能调用方法.功能 ...
- Servlet中的常用类以及常用方法
A:ServletConfig:用于读取Servlet在web.xml中配置的一些信息. getServletName(); getInitParameter();只能是Servlet自身下的参数设置 ...
- python 中一些常用的内置函数
一.常用内置函数 abs(x) 返回绝对值,参数为int float,非字符只能num all(iterable) 如果迭代对象里面的所有值都为真就返回True.all([1, 2, -7]) --- ...
- C++中怎么获取类的成员函数的函数指针?
用一个实际代码来说明. class A { public: staticvoid staticmember(){cout<<"static"<<endl;} ...
- Lua中的常用语句结构以及函数
1.Lua中的常用语句结构介绍 --if 语句结构,如下实例: gTable = {} ] ] then ]) == gTable[] then ]) else print("unkown ...
随机推荐
- win7 与 Ubuntu 16.04 文件传送
win7 与 Ubuntu 16.04 文件传送 环境:主机系统为win7,虚拟机为vmware12, 虚拟系统为ubuntu 16.04 方案一: 通过虚拟机vmware的共享文件夹实现. 方案二: ...
- Android系统--输入系统(二)必备Linux知识_实现inotify_epoll.c
Android系统--输入系统(二)必备Linux知识_实现inotify_epoll.c 课后作业 1. 编写 inotify_epoll.c, 用它来监测tmp/目录: 有文件被创建/删除, 有文 ...
- MongoDB快速入门(八)- 删除文档
删除文档 MongoDB 的 remove()方法用于从集合中删除文档.remove()方法接受两个参数.一个是标准缺失,第二是justOne标志 deletion criteria : 根据文件(可 ...
- showModalDialog改进版,包括Chrome下的特殊处理
父页面: if(window.ActiveXObject){ //IE $("#choose_entp").click(function(){ ...
- R语言笔记004——R批量读取txt文件
R批量读取txt文件 本文数据,代码都是参考的是大音如霜公众号,只是自己跟着做了一遍. path<-'C:\\Users\\Administrator\\Desktop\\docs' docs& ...
- Spring初学之bean之间的关系和bean的作用域
一.bean之间的关系 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...
- keystone v2/v3
Changing from APIv2.0 to APIv3 in Keystone - Openstack Juno on Ubuntu 1. 更换v3 的policy文件 mv /etc/keys ...
- mac上完整卸载删除:android studio方案
如果你是mac ,你删除as ,删不干净也正常,你会发现安装的时候,前面的东西也在.配置文件在,会导致你以前的错误不想要的东西都在. 废话不多说,复制粘贴!!~~~~~~~~ 第一步: 复制粘贴!! ...
- ASP.NET MVC中加入Web Forms
目的 有时候在一个ASP.NET MVC项目发布之后,又需要添加动态页面,同时又不想重新在源代码中添加view,那么这时候就要用上Web Forms了. 步骤 1.在项目根目录添加一个文件夹,在文件夹 ...
- sass入门篇
CSS 预处理器定义了一种新的语言,其基本思想是,用一种专门的编程语言,为 CSS 增加了一些编程的特性,将 CSS 作为目标生成文件,然后开发者就只要使用这种语言进行编码工作. 通俗的说,“CSS ...