net中用户登录验证和基页面类
if (blIsUser) {
//将用户名称存入cookie FormsAuthentication.SetAuthCookie(strLogName, false);
if (IsAdmin(strLogName)) { Response.Redirect("AdminGround/adminmain.aspx"); } else { Response.Redirect("defaultList.aspx"); } } else { this.Alert("登陆失败"); return; }
基页面类
using System;
using System.Data;
using System.Configuration;
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.Data.SqlClient;
using PycSQLHelper;
public class ThisWebBase : System.Web.UI.Page
{
public ThisWebBase()
{
}
protected override void OnInit(EventArgs e)
{
if (!User.Identity.IsAuthenticated)
{
Response.Clear();
Response.Write("您还未登陆");
Response.End();
}
string strFirstFolder = this.Request.AppRelativeCurrentExecutionFilePath.Substring(2);
if (strFirstFolder.Contains("/"))
{
strFirstFolder = strFirstFolder.Substring(0, strFirstFolder.IndexOf("/"));
}
if (strFirstFolder.ToLower() == ConfigurationManager.AppSettings["AdminFolder"].ToLower())
{
if (!CheckUserIsAdmin(GetCurrentUser()))
{
Response.Clear();
Response.Write("您不是管理员");
Response.End();
}
}
base.OnInit(e);
}
private bool CheckUserIsAdmin(string strLogName)
{
string strSql = "select count(*) from useres where logname=@logname and role=1";
if (Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnString, CommandType.Text, strSql, new SqlParameter("@logname", strLogName))) > 0)
return true;
return false;
}
/// <summary>
/// 返回当前登录人用户名
/// </summary>
/// <returns></returns>
public string GetCurrentUser()
{
string strName = System.Web.HttpContext.Current.User.Identity.Name.Trim();
if (strName != null)
{
int index = strName.IndexOf("\\");
if (index > -1)
{
strName = strName.Substring(index + 1);
}
}
return strName;
}
/// <summary>
/// 验证传入的ID类字符串是否有效
/// </summary>
/// <param name="strID">传入字符串</param>
/// <param name="intID">传出ID</param>
protected void checkID(string strID, out int intID)
{
if (!int.TryParse(strID, out intID) && strID != null)
{
Response.Clear();
Response.Write("参数错误!");
Response.End();
}
}
/// <summary>
/// 绑定下拉菜单列表
/// </summary>
/// <param name="drp">要绑定的菜单</param>
/// <param name="dt">数据源</param>
/// <param name="strText">显示列</param>
/// <param name="strValue">值列</param>
protected void Drp_Bind(DropDownList drp, DataTable dt, string strText, string strValue, bool blInsert)
{
drp.Items.Clear();
if (dt.Rows.Count > 0)
{
drp.DataSource = dt;
drp.DataTextField = strText;
drp.DataValueField = strValue;
drp.DataBind();
if (blInsert)
{
drp.Items.Insert(0, new ListItem("--", "0"));
}
}
else
{
drp.Items.Add(new ListItem("--", "0"));
}
}
/// <summary>
/// 提示
/// </summary>
/// <param name="strAlert">要显示的信息</param>
protected void Alert(string strAlert)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type=\"text/javascript\">alert('" + strAlert + "')</script>");
}
/// <summary>
/// 提示关闭
/// </summary>
/// <param name="strAlert">要显示的信息</param>
protected void AlertClose(string strAlert)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type=\"text/javascript\">alert('" + strAlert + "');window.close();</script>");
}
/// <summary>
/// 关闭页面
/// </summary>
protected void Close()
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type=\"text/javascript\">window.close();</script>");
}
/// <summary>
/// 返回值
/// </summary>
protected void returnValue(string managers)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type=\"text/javascript\">window.returnValue='" + managers + "';window.close();</script>");
}
/// <summary>
/// 提示并刷新当前页
/// </summary>
/// <param name="strAlert">要显示的信息</param>
protected void AlertRefresh(string strAlert)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type=\"text/javascript\">alert('" + strAlert + "');window.location.href=window.location;</script>");
}
/// <summary>
/// 提示并刷新父页面关闭本页面
/// </summary>
/// <param name="strAlert">要显示的信息</param>
protected void AlertRefreshParent(string strAlert)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type=\"text/javascript\">alert('" + strAlert + "');window.close();window.opener.location.reload();</script>");
}
/// <summary>
/// 提示并跳转
/// </summary>
/// <param name="strAlert">要显示的信息</param>
/// <param name="strURL">要跳转的页面</param>
protected void AlertRefresh(string strAlert, string strURL)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type=\"text/javascript\">alert('" + strAlert + "');window.location.href='" + strURL + "';</script>");
}
/// <summary>
/// 运行javascript函数
/// </summary>
/// <param name="strAlert">函数名称</param>
protected void RunScriptFunction(string strFunction)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type=\"text/javascript\">" + strFunction + "</script>");
}
/// <summary>
/// 提示框
/// </summary>
/// <param name="strConfirm">提示信息</param>
protected void Confirm(string strConfirm, string id)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type=\"text/javascript\">if(confirm('" + strConfirm + "') == true){window.document.all('" + id + "').click();};</script>");
}
/// <summary>
/// 截取字符串
/// </summary>
/// <param name="strInput"></param>
/// <param name="intLen"></param>
/// <returns></returns>
public string cutString(string strInput, int intLen)
{
strInput = strInput.Trim();
byte[] myByte = System.Text.Encoding.Default.GetBytes(strInput);
if (myByte.Length > intLen)
{
//截取操作
string resultStr = "";
for (int i = 0; i < strInput.Length; i++)
{
byte[] tempByte = System.Text.Encoding.Default.GetBytes(resultStr);
if (tempByte.Length < intLen)
{
resultStr += strInput.Substring(i, 1);
}
else
{
break;
}
}
return resultStr + "...";
}
else
{
return strInput;
}
}
/// <summary>
/// 加密
/// </summary>
/// <param name="pToEncrypt">需要加密的字符串</param>
/// <param name="sKey">密钥</param>
/// <returns></returns>
public string Encrypt(string pToEncrypt, string sKey)
{
try
{
if (pToEncrypt == "")
return "";
else
{
System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
des.Key = System.Text.Encoding.UTF8.GetBytes(sKey);
des.Mode = System.Security.Cryptography.CipherMode.ECB;
System.Security.Cryptography.ICryptoTransform DESEncrypt = des.CreateEncryptor();
byte[] Buffer = System.Text.Encoding.UTF8.GetBytes(pToEncrypt);
string retValue = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
DESEncrypt.Dispose();
return retValue;
}
}
catch { return ""; }
}
/// <summary>
/// 解密
/// </summary>
/// <param name="pToDecrypt">需要解密的字符串</param>
/// <param name="sKey">密钥</param>
/// <returns></returns>
public string Decrypt(string pToDecrypt, string sKey)
{
try
{
if (pToDecrypt == "")
return "";
else
{
System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
des.Key = System.Text.Encoding.UTF8.GetBytes(sKey);
des.Mode = System.Security.Cryptography.CipherMode.ECB;
des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
System.Security.Cryptography.ICryptoTransform DESDecrypt = des.CreateDecryptor();
string result = "";
byte[] Buffer = Convert.FromBase64String(pToDecrypt);
result = System.Text.Encoding.UTF8.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
DESDecrypt.Dispose();
return result;
}
}
catch { return ""; }
}
/// <summary>
/// 返回选项前标记
/// </summary>
/// <param name="intFlag">传进的选项序号</param>
/// <returns></returns>
public string returnOptionIndex(int intFlag)
{
string[] strOptionIndex = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N" };
if (intFlag >= 0 && intFlag < 15)
return strOptionIndex[intFlag];
return " ";
}
/// <summary>
/// 返回题型前汉字序号
/// </summary>
public string []returnTypeIndex
{
get { string[] str = { "一", "二", "三", "四", "五" }; return str; }
}
}
浙公网安备 33010602011771号