📄 loginchk.cs
字号:
using System;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Web.Security;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using liuwei.FrameWork.DB;
namespace liuwei.FrameWork
{
public class LoginChk
{
private LoginChk(){}
#region 这里接受参数,然后传给相应的方法
/// <summary>
/// 传入登陆的验证,返回给login.aspx.cs一个bool值
/// </summary>
//如果不保存登陆
public static bool Authenticate(string username, string password)
{
return Authenticate(username,password,false);
}
//如果保存登陆
public static bool Authenticate(string username, string password,bool persist)
{
//用这个方法到数据库中提取数据,使用存储过程
if(IsValidUser(username,password))
{
SetTicket(username,persist);
return true;
}
return false;
}
#endregion
#region 登陆成功,为用户设置验证票
private static void SetTicket(string username, bool persist)
{
FormsAuthentication.SetAuthCookie(username,persist);
}
#endregion
#region 判断用户登陆,数据库提取信息 IsValidUser(string username, string password)
/// <summary>
/// 判断用户
/// </summary>
public static bool IsValidUser(string username, string password)
{
//根据参数检索数据库中有无此用户
string sql = "ceocio_GetUser";
string conn = ConfigurationSettings.AppSettings["strConnection"];
SqlParameter[] p =
{
SqlHelper.MakeInParam("@username",SqlDbType.NVarChar,50,username)
};
DataTable dt = SqlHelper.ExecuteDataTable(conn,CommandType.StoredProcedure,sql,p);
int resultcount = dt.Rows.Count;
if (resultcount==1)
{
string resultpassword=dt.Rows[0]["password"].ToString();
password = Encrypt(password);
dt.Clear();
dt.Dispose();
//返回值,生成form验证票
return string.Compare(password,resultpassword,false)==0;
}
dt.Clear();
dt.Dispose();
return false;
}
#endregion
#region 处理密码 Hash密码 Encrypt(string password)
/// <summary>
/// 处理密码 Hash密码
/// </summary>
/// <param name="password">Supplied Password</param>
/// <returns>Encrypted (Hashed) value</returns>
public static string Encrypt(string password)
{
// Force the string to lower case
//
password = password.ToLower();
Byte[] clearBytes = new UnicodeEncoding().GetBytes(password);
Byte[] hashedBytes = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
return BitConverter.ToString(hashedBytes);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -