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 ...
随机推荐
- python:列表的方法
注意:在列表的类方法一般是没有返回值的,如果将处理过的列表给新变量,新变量是空类型.如:>>>a=[1,2]>>>b=a.append(3)>>> ...
- 第四篇、linux系统文件属性三
一.linux文件属性之文件权限体系介绍 二.linux中连接介绍 三.软连接 四.图解 五文件删除原理 主要内容
- Shell脚本报错--syntax error near unexpected token for((i=0;i<$length;i++))
现象: shell脚本使用Nodepad++进行本地编辑,在编辑后上传到linux机器进行执行时提示“syntax error near unexpected token for((i=0;i< ...
- Linux Ctrl+Z VS Ctrl+C 以及+Z的使用方法
问题及处理: Ctrl+Z是将任务中断,但是此任务并没有结束,他仍然在进程中他只是维持挂起的状态,用户可以使用fg/bg操作继续前台或后台的任务,fg命令重新启动前台被中断的任务,bg命令把被中断的任 ...
- ZooKeeper学习第八期---ZooKeeper伸缩性
转:http://www.cnblogs.com/sunddenly/p/4143306.html 一.ZooKeeper中Observer 1.1 ZooKeeper角色 经过前面的介绍,我想大家都 ...
- JavaScript -- 练习 window 流氓广告
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- linux图形界面基本知识(X、X11、Xfree86、Xorg、GNOME、KDE之间的关系)
linux图形界面基本知识(X.X11.Xfree86.Xorg.GNOME.KDE之间的关系)(转自互联网) LINUX初学者经常分不清楚linux和X之间,X和Xfree86之间,X和KDE,GN ...
- 新建Maven项目Index.jsp文件报错解决方法
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path in ...
- Redis作为缓存:实战自我总结(转载)
转载:[http://www.tuicool.com/articles/zayY7v] redis缓存服务器笔记 redis是一个高性能的key-value存储系统,能够作为缓存框架和队列.但是由 ...
- LeetCode OJ:Length of Last Word(最后一个词的长度)
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...