using System;
using System.Collections.Generic;
using System.Web;
using System.Data.OleDb;
using System.Text;
using System.Data;
using System.Web.Security;
namespace GD.App_Code
{
public class UserInfo
{
/// <summary>
/// 验证用户登陆
/// </summary>
/// <param name="username">用户名称</param>
/// <param name="password">用户密码</param>
/// <returns></returns>
public static bool ValidUser(string username, string password, int power)
{
password = DESEncrypt.Encrypt(password);
StringBuilder sb = new StringBuilder();
if (power == (int)UserPower.customer)
{
sb.Append("select [UserName] from Customer where [UserName]=@username ");
}
else
{
sb.Append(string.Format("select [UserName] from Manager where [UserName]=@username and [Power]='{0}' ",power));
}
sb.Append("and [Password]=@password");
OleDbParameter[] param =
{
OleDbHelper.GetParameter("@username",OleDbType.Char,50,"[UserName]",username),
OleDbHelper.GetParameter("@password",OleDbType.Char,50,"[Password]",password)
};
DataTable table = OleDbHelper.Query(sb.ToString(), param);
if (table.Rows.Count > 0)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 更改用户密码
/// </summary>
/// <param name="username">用户名称</param>
/// <param name="password">用户密码</param>
/// <returns></returns>
public static bool ChangePsd(string username, string password, int power)
{
password = DESEncrypt.Encrypt(password);
StringBuilder sb = new StringBuilder();
if (power == (int)UserPower.customer)
{
sb.Append(string.Format("update Customer set [Password]='{0}' where [UserName]='{1}' ", password, username));
}
else
{
sb.Append(string.Format("update Manager set [Password]='{0}' where [UserName]='{1}' ", password, username));
}
int i = OleDbHelper.ExecuteSql(sb.ToString());
if (i > 0)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 设置COOKIE
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
public static void SetCookie(string username, int power)
{
int userid = -1;
bool bHas = true;
if (power == (int)UserPower.customer)
{
App_Code.Customer customer = new Customer(username);
userid = customer.ID;
if (customer.UserName == string.Empty)
{
bHas = false;
}
}
else
{
Manager manager = new Manager(username);
userid = manager.UserID;
if (manager.UserName == string.Empty)
{
bHas = false;
}
}
if (bHas)
{
string userData = username.Replace(@"\", @"\\") + "#" + power.ToString();
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userid.ToString(), DateTime.Now, DateTime.Now.AddMinutes(60), false, userData, FormsAuthentication.FormsCookiePath);
string encTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie newCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
HttpContext.Current.Response.Cookies.Add(newCookie);
}
}
/// <summary>
/// 判断用户是否登录
/// </summary>
/// <returns>已登录返回true</returns>
public static bool IsLogin()
{
return HttpContext.Current.User.Identity.IsAuthenticated;
}
/// <summary>
/// 退出登录
/// </summary>
public static void Logout()
{
FormsAuthentication.SignOut();
}
/// <summary>
/// 获取登录的用户ID
/// </summary>
/// <returns></returns>
public static int getUserID()
{
if (IsLogin())
{
return Convert.ToInt32(HttpContext.Current.User.Identity.Name);
}
else
{
return -1;
}
}
/// <summary>
/// 获得用户名
/// </summary>
/// <returns></returns>
public static string GetUserName()
{
if (IsLogin())
{
string strUserData = ((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;
string[] UserData = strUserData.Split(new string[] { @"#" }, StringSplitOptions.RemoveEmptyEntries);
if (UserData.Length > 0)
{
return UserData[0].Replace(@"\\", @"\");
}
else
return "";
}
else
{
return "";
}
}
/// <summary>
/// 返回用户的角色
/// </summary>
/// <returns></returns>
public static int GetUserRole()
{
if (IsLogin())
{
string strUserData = ((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;
string[] UserData = strUserData.Split(new string[] { @"#" }, StringSplitOptions.RemoveEmptyEntries);
if (UserData.Length > 0)
{
return Int32.Parse(UserData[1]);
}
else
{
return -1;
}
}
else
{
return -1;
}
}
}
public enum UserPower
{
customer,
salesman,
manager
}
}