自己写的基类:Forms身份验证类、客户端事件类,序列化类下载
最近写了几个类,给刚刚入门的同学们参考吧!
Forms身份验证类:
/*创建人:阿泰 *创建时间:2011-9-15 *说明:获取、写入forms身份验证所存储的票据,为Forms身份验证登录所用(VS2010版本) */ using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; /// <summary> ///FormsData 的摘要说明 /// </summary> public class FormsData { public FormsData() { // //TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 获取登录用户的权限 /// </summary> /// <param name="UserName">用户名</param> /// <param name="IsAdmin">否0,是1</param> /// <param name="IsSuperAdmin">否0,是1</param> /// <returns>返回含有权限的用户登录的票据</returns> public static string GetUserData(string UserName,string IsAdmin,string IsSuperAdmin) { string userData = UserName + "," + IsAdmin+","+IsSuperAdmin; return userData; } /// <summary> /// 获取forms身份验证所存储的票据 /// </summary> /// <param name="i">一般情况下,0为用户名,1为是否普通管理员,2为是否超级管理员</param> /// <returns>返回forms身份验证票据</returns> public static string GetFormsTicket(int i) { if (HttpContext.Current.Request.IsAuthenticated) { FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity; string[] userData = identity.Ticket.UserData.Split(','); return userData[i].ToString(); } else { return ""; } } /// <summary> /// 写入forms身份验证所存储的票据,一般为登录所用 /// </summary> /// <param name="username">用户名</param> /// <param name="IsAdmin">是否管理员,是则为1,否则为0</param> /// <param name="IsSuperAdmin">是否超级管理员,是则为1,否则为0</param> /// <param name="expirationDay">票据的期限,以“天”为单位</param> public static void SetFormsTicket(string username, string IsAdmin, string IsSuperAdmin, int expirationDay) { //获取票据 string userData = GetUserData(username,IsAdmin,IsSuperAdmin); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddDays(expirationDay), true, userData); string authTicket = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, authTicket); cookie.Expires = ticket.Expiration; HttpContext.Current.Response.SetCookie(cookie); } /// <summary> /// 写入forms身份验证所存储的票据,一般为登录所用(重载版本,无权限控制的登录) /// </summary> /// <param name="username">用户名</param> /// <param name="expirationDay">票据的期限,以“天”为单位</param> public static void SetFormsTicket(string username, int expirationDay) { SetFormsTicket(username, "0", "0", expirationDay); } }
客户端事件类:
/*创建人:陈盛泰,阿泰 *创建时间:2011-7-15 *说明:弹出对话框的类(VS2010版本) */ using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; /// <summary> ///弹出对话框的类 /// </summary> public class Jscript { public Jscript() { // //TODO: 在此处添加构造函数逻辑 // } //弹出对话框 /// <summary> /// 弹出对话框 /// </summary> /// <param name="msg">输入弹出内容</param> /// <param name="page">指在那个页面,一般用this,表示当前页</param> public static void AlertMsg(string msg, Page page) { string js = @"<script>alert('" + msg + "')</script>"; page.ClientScript.RegisterStartupScript(page.GetType(), "提示 ", js); } //弹出对话框并转向其他页面 /// <summary> /// 弹出对话框并转向其他页面 /// </summary> /// <param name="msg">输入弹出内容</param> /// <param name="url">转向网页路径</param> /// <param name="page">指在那个页面,一般用this,表示当前页</param> public static void AlertMsg(string msg, string url, Page page) { string js = @"<script>alert('" + msg + "');location.href='" + url + "'</script>"; page.ClientScript.RegisterStartupScript(page.GetType(), "提示 ", js); } //跳转页面 /// <summary> /// 跳转页面 /// </summary> /// <param name="url">转向网页路径</param> /// <param name="page">指在那个页面,一般用this,表示当前页</param> public static void windowOpen(string url, Page page) { string js = @"<script>window.open('" + url + "','_blank');</script>"; page.ClientScript.RegisterStartupScript(page.GetType(), "", js); } //弹出提示对话框后关闭窗口 /// <summary> /// 弹出提示对话框后关闭窗口 /// </summary> /// <param name="msg">提示文字</param> /// <param name="page">一边用this,表示当前页</param> public static void windowClose(string msg, Page page) { string js = @"<script>alert('" + msg + "');window.close();</script>"; page.ClientScript.RegisterStartupScript(page.GetType(), "", js); } }
序列化类:
//----------------------------------------------------------------------- // Coding by: AC Created date: 2010-8-5 13:10:09 // Description: // Others desc: // Alter History: // [By] [Date] [Version] [Purpose] // AC 2010-8-5 13:10:09 1.0 Create //---------------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Reflection; using System.Text; using System.Data; /// <summary> /// JSON序列器 /// </summary> public class JSONSerializer { private readonly StringBuilder _output = new StringBuilder(); public static string ToJSON(object obj) { return new JSONSerializer().ConvertToJSON(obj); } private string ConvertToJSON(object obj) { WriteValue(obj); return _output.ToString(); } private void WriteValue(object obj) { if (obj == null) _output.Append("null"); else if (obj is sbyte || obj is byte || obj is short || obj is ushort || obj is int || obj is uint || obj is long || obj is ulong || obj is decimal || obj is double || obj is float) _output.Append(Convert.ToString(obj, NumberFormatInfo.InvariantInfo)); else if (obj is bool) _output.Append(obj.ToString().ToLower()); else if (obj is char || obj is Enum || obj is Guid) WriteString("" + obj); else if (obj is DateTime) WriteString(((DateTime)obj).ToString("yyyy-MM-dd")); else if (obj is string) WriteString((string)obj); else if (obj is IDictionary) WriteDictionary((IDictionary)obj); else if (obj is Array || obj is IList || obj is ICollection) WriteArray((IEnumerable)obj); else if (obj is DataTable) WriteDataTable((DataTable)obj); else WriteObject(obj); } private void WriteObject(object obj) { _output.Append("{ "); bool pendingSeparator = false; foreach (FieldInfo field in obj.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)) { if (pendingSeparator) _output.Append(" , "); WritePair(field.Name, field.GetValue(obj)); pendingSeparator = true; } foreach (PropertyInfo property in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (!property.CanRead) continue; if (pendingSeparator) _output.Append(" , "); WritePair(property.Name, property.GetValue(obj, null)); pendingSeparator = true; } _output.Append(" }"); } private void WritePair(string name, object value) { WriteString(name); _output.Append(" : "); WriteValue(value); } private void WriteArray(IEnumerable array) { _output.Append("[ "); bool pendingSeperator = false; foreach (object obj in array) { if (pendingSeperator) _output.Append(','); WriteValue(obj); pendingSeperator = true; } _output.Append(" ]"); } private void WriteDictionary(IDictionary dic) { _output.Append("{ "); bool pendingSeparator = false; foreach (DictionaryEntry entry in dic) { if (pendingSeparator) _output.Append(" , "); WritePair(entry.Key.ToString(), entry.Value); pendingSeparator = true; } _output.Append(" }"); } private void WriteString(string s) { _output.Append('\"'); foreach (char c in s) { switch (c) { case '\t': _output.Append("\\t"); break; case '\r': _output.Append("\\r"); break; case '\n': _output.Append("\\n"); break; case '"': case '\\': _output.Append("\\" + c); break; default: { if (c >= ' ' && c < 128) _output.Append(c); else _output.Append("\\u" + ((int)c).ToString("X4")); } break; } } _output.Append('\"'); } private void WriteDataTable(DataTable table) { List<Hashtable> data = new List<Hashtable>(); foreach (DataRow row in table.Rows) { Hashtable dic = new Hashtable(); foreach (DataColumn c in table.Columns) { dic.Add(c.ColumnName, row[c]); } data.Add(dic); } WriteValue(data); } }
若要使用的话,就在这里下载吧: