登录代码,程序不是作文
2010-01-18 15:38
274 查看
//原来字写错了,太汗了,代码
一,
using System;
using System.Collections;
using System.Security;
using System.Security.Cryptography;
using System.Text;
namespace Permission.WebAdmin
{
/// <summary>
/// 用户对象的安全上下文信息
/// </summary>
public class AccountsPrincipal : System.Security.Principal.IPrincipal
{
#region 属性
protected System.Security.Principal.IIdentity identity;
protected ArrayList permissionList;
protected ArrayList permissionListid;
protected ArrayList roleList;
/// <summary>
/// 当前用户的所有角色
/// </summary>
public ArrayList Roles
{
get
{
return roleList;
}
}
/// <summary>
/// 当前用户拥有的权限列表
/// </summary>
public ArrayList Permissions
{
get
{
return permissionList;
}
}
/// <summary>
/// 当前用户拥有的权限ID列表
/// </summary>
public ArrayList PermissionsID
{
get
{
return permissionListid;
}
}
// IPrincipal Interface Requirements:
/// <summary>
/// 当前用户的标识对象
/// </summary>
public System.Security.Principal.IIdentity Identity
{
get
{
return identity;
}
set
{
identity = value;
}
}
#endregion
/// <summary>
/// 根据用户编号构造
/// </summary>
public AccountsPrincipal(int userID)
{
identity = new SiteIdentity(userID);
permissionList = AccountsPrincipalDLL.GetEffectivePermissionList(userID);
permissionListid = AccountsPrincipalDLL.GetEffectivePermissionListID(userID);
roleList = AccountsPrincipalDLL.GetUserRoles(userID);
}
/// <summary>
/// 根据用户名构造
/// </summary>
public AccountsPrincipal(string userName)
{
identity = new SiteIdentity(userName);
permissionList = AccountsPrincipalDLL.GetEffectivePermissionList(((SiteIdentity)identity).UserID);
permissionListid = AccountsPrincipalDLL.GetEffectivePermissionListID(((SiteIdentity)identity).UserID);
roleList = AccountsPrincipalDLL.GetUserRoles(((SiteIdentity)identity).UserID);
}
/// <summary>
/// 当前用户是否属于指定名称的角色
/// </summary>
public bool IsInRole(string role)
{
return roleList.Contains(role);
}
/// <summary>
/// 当前用户是否拥有指定名称的权限
/// </summary>
public bool HasPermission(string permission)
{
return permissionList.Contains(permission);
}
/// <summary>
/// 当前用户是否拥有指定的权限
/// </summary>
public bool HasPermissionID(int permissionid)
{
return permissionListid.Contains(permissionid);
}
/// <summary>
/// 验证登录信息
/// </summary>
public static AccountsPrincipal ValidateLogin(string userName, string password)
{
int newID;
byte[] cryptPassword = EncryptPassword(password);
Data.User dataUser = new Data.User();
if ((newID = dataUser.ValidateLogin(userName, cryptPassword)) > 0)
return new AccountsPrincipal(newID);
else
return null;
}
/// <summary>
/// 密码加密
/// </summary>
public static byte[] EncryptPassword(string password)
{
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] hashBytes = encoding.GetBytes(password);
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] cryptPassword = sha1.ComputeHash(hashBytes);
return cryptPassword;
}
}
}
二
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Permission.Common;
namespace Permission.WebAdmin
{
/// <summary>
/// 页面基类
/// </summary>
public class PageBase : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(this.Page_Load);
this.Error += new System.EventHandler(this.Page_Error);
}
#region 权限检查
/// <summary>
/// 页面访问权限ID。
/// </summary>
public virtual int PermissionID
{
get { return -1; }
}
public AccountsPrincipal CurrentPrincipal
{
get
{
if (Context.User.Identity.IsAuthenticated)
{
AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
return user;
}
return null;
}
}
/// <summary>
/// 当前用户信息
/// </summary>
public Tb_Accounts_Users CurrentUser
{
get
{
if (CurrentPrincipal == null)
{
return null;
}
if (Session["UserInfo"] == null)
{
LTP.Accounts.Bus.User currentUser = new LTP.Accounts.Bus.User(CurrentPrincipal);
Session["UserInfo"] = currentUser;
}
return Session["UserInfo"] as Tb_Accounts_Users;
}
}
#endregion
#region 页面事件
private void Page_Load(object sender, System.EventArgs e)
{
//网站域名或虚拟目录
string virtualPath = ConfigurationManager.AppSettings.Get("VirtualPath");
//登录页地址
string loginPage = ConfigurationManager.AppSettings.Get("LoginPage");
if (Context.User.Identity.IsAuthenticated)
{
AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
if ((PermissionID != -1) && (!user.HasPermissionID(PermissionID)))
{
Response.Clear();
Response.Write("<script defer>window.alert('您没有权限进入本页!');history.back();</script>");
Response.End();
}
}
else
{
FormsAuthentication.SignOut();
Session.Clear();
Session.Abandon();
Response.Clear();
Response.Write("<script defer>window.alert('您没有权限进入本页或当前登录用户已过期!http://www.cnblogs.com/hsapphire/admin/file://n/请重新登录或与管理员联系!');parent.location='" + virtualPath + "/" + loginPage + "';</script>");
Response.End();
}
}
protected void Page_Error(object sender, System.EventArgs e)
{
string errMsg = "";
Exception currentError = Server.GetLastError();
errMsg += "系统发生错误:<br/>" +
"错误地址: " + Request.Url.ToString() + "<br/>" +
"错误信息: " + currentError.Message.ToString() + "<br/>";
Response.Write(errMsg);
Server.ClearError();//要注意这句代码的使用,清除异常。
}
#endregion
#region URL参数
public virtual string Name
{
get
{
if ((Request["name"] != null) && (Request["name"].ToString() != ""))
{
return Request.QueryString["name"].Trim();
}
return "";
}
}
#endregion
}
}
三。
using System;
using System.Collections.Generic;
using System.Text;
using Permission.Common;
using System.Security.Cryptography;
namespace Permission.WebAdmin
{
/// <summary>
/// 当前用户的标识对象
/// </summary>
[Serializable]
public class SiteIdentity : System.Security.Principal.IIdentity
{
#region 用户属性
private string userName;
private string trueName;
private string email;
private byte[] password;
private int userID;
private string sex;
/// <summary>
/// 用户名
/// </summary>
public string UserName
{
get
{
return userName;
}
}
/// <summary>
/// 真实姓名
/// </summary>
public string TrueName
{
get
{
return trueName;
}
}
/// <summary>
/// 邮箱
/// </summary>
public string Email
{
get
{
return email;
}
}
/// <summary>
/// 用户编号
/// </summary>
public int UserID
{
get
{
return userID;
}
}
/// <summary>
/// 密码
/// </summary>
public byte[] Password
{
get
{
return password;
}
}
/// <summary>
/// 性别
/// </summary>
public string Sex
{
get
{
return sex;
}
}
#endregion
#region IIdentity interface requirments:
/// <summary>
/// 当前用户的名称
/// </summary>
public string Name
{
get
{
return userName;
}
}
/// <summary>
/// 获取所使用的身份验证的类型。
/// </summary>
public string AuthenticationType
{
get
{
return "Custom Authentication";
}
set
{
// do nothing
}
}
/// <summary>
/// 是否验证了用户
/// </summary>
public bool IsAuthenticated
{
get
{
return true;
}
}
#endregion
/// <summary>
/// 根据用户名构造
/// </summary>
public SiteIdentity(string currentUserName)
{
Tb_Accounts_Users entityUser=BllAccess . UserDLL.UserGetModelByUserName(currentUserName);
userName = currentUserName;
trueName = entityUser.TrueName;
email = entityUser.Email;
userID = entityUser.UserID;
password = entityUser.Password;
sex = entityUser.Sex;
}
/// <summary>
/// 根据用户ID构造
/// </summary>
public SiteIdentity(int currentUserID)
{
Tb_Accounts_Users entityUser = UserDLL.UserGetModelByUserID(currentUserID);
userName = entityUser.UserName;
trueName = entityUser.TrueName;
email = entityUser.Email;
userID = currentUserID;
password = entityUser.Password;
sex = entityUser.Sex;
}
/// <summary>
/// 检查当前用户对象密码
/// </summary>
public int TestPassword(string password)
{
// At some point, we may have a more complex way of encrypting or storing the passwords
// so by supplying this procedure, we can simply replace its contents to move password
// comparison to the database (as we've done below) or somewhere else (e.g. another
// web service, etc).
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] hashBytes = encoding.GetBytes(password);
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] cryptPassword = sha1.ComputeHash(hashBytes);
return UserDLL.TestPassword(userID, cryptPassword);
}
}
}
一,
using System;
using System.Collections;
using System.Security;
using System.Security.Cryptography;
using System.Text;
namespace Permission.WebAdmin
{
/// <summary>
/// 用户对象的安全上下文信息
/// </summary>
public class AccountsPrincipal : System.Security.Principal.IPrincipal
{
#region 属性
protected System.Security.Principal.IIdentity identity;
protected ArrayList permissionList;
protected ArrayList permissionListid;
protected ArrayList roleList;
/// <summary>
/// 当前用户的所有角色
/// </summary>
public ArrayList Roles
{
get
{
return roleList;
}
}
/// <summary>
/// 当前用户拥有的权限列表
/// </summary>
public ArrayList Permissions
{
get
{
return permissionList;
}
}
/// <summary>
/// 当前用户拥有的权限ID列表
/// </summary>
public ArrayList PermissionsID
{
get
{
return permissionListid;
}
}
// IPrincipal Interface Requirements:
/// <summary>
/// 当前用户的标识对象
/// </summary>
public System.Security.Principal.IIdentity Identity
{
get
{
return identity;
}
set
{
identity = value;
}
}
#endregion
/// <summary>
/// 根据用户编号构造
/// </summary>
public AccountsPrincipal(int userID)
{
identity = new SiteIdentity(userID);
permissionList = AccountsPrincipalDLL.GetEffectivePermissionList(userID);
permissionListid = AccountsPrincipalDLL.GetEffectivePermissionListID(userID);
roleList = AccountsPrincipalDLL.GetUserRoles(userID);
}
/// <summary>
/// 根据用户名构造
/// </summary>
public AccountsPrincipal(string userName)
{
identity = new SiteIdentity(userName);
permissionList = AccountsPrincipalDLL.GetEffectivePermissionList(((SiteIdentity)identity).UserID);
permissionListid = AccountsPrincipalDLL.GetEffectivePermissionListID(((SiteIdentity)identity).UserID);
roleList = AccountsPrincipalDLL.GetUserRoles(((SiteIdentity)identity).UserID);
}
/// <summary>
/// 当前用户是否属于指定名称的角色
/// </summary>
public bool IsInRole(string role)
{
return roleList.Contains(role);
}
/// <summary>
/// 当前用户是否拥有指定名称的权限
/// </summary>
public bool HasPermission(string permission)
{
return permissionList.Contains(permission);
}
/// <summary>
/// 当前用户是否拥有指定的权限
/// </summary>
public bool HasPermissionID(int permissionid)
{
return permissionListid.Contains(permissionid);
}
/// <summary>
/// 验证登录信息
/// </summary>
public static AccountsPrincipal ValidateLogin(string userName, string password)
{
int newID;
byte[] cryptPassword = EncryptPassword(password);
Data.User dataUser = new Data.User();
if ((newID = dataUser.ValidateLogin(userName, cryptPassword)) > 0)
return new AccountsPrincipal(newID);
else
return null;
}
/// <summary>
/// 密码加密
/// </summary>
public static byte[] EncryptPassword(string password)
{
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] hashBytes = encoding.GetBytes(password);
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] cryptPassword = sha1.ComputeHash(hashBytes);
return cryptPassword;
}
}
}
二
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Permission.Common;
namespace Permission.WebAdmin
{
/// <summary>
/// 页面基类
/// </summary>
public class PageBase : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(this.Page_Load);
this.Error += new System.EventHandler(this.Page_Error);
}
#region 权限检查
/// <summary>
/// 页面访问权限ID。
/// </summary>
public virtual int PermissionID
{
get { return -1; }
}
public AccountsPrincipal CurrentPrincipal
{
get
{
if (Context.User.Identity.IsAuthenticated)
{
AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
return user;
}
return null;
}
}
/// <summary>
/// 当前用户信息
/// </summary>
public Tb_Accounts_Users CurrentUser
{
get
{
if (CurrentPrincipal == null)
{
return null;
}
if (Session["UserInfo"] == null)
{
LTP.Accounts.Bus.User currentUser = new LTP.Accounts.Bus.User(CurrentPrincipal);
Session["UserInfo"] = currentUser;
}
return Session["UserInfo"] as Tb_Accounts_Users;
}
}
#endregion
#region 页面事件
private void Page_Load(object sender, System.EventArgs e)
{
//网站域名或虚拟目录
string virtualPath = ConfigurationManager.AppSettings.Get("VirtualPath");
//登录页地址
string loginPage = ConfigurationManager.AppSettings.Get("LoginPage");
if (Context.User.Identity.IsAuthenticated)
{
AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
if ((PermissionID != -1) && (!user.HasPermissionID(PermissionID)))
{
Response.Clear();
Response.Write("<script defer>window.alert('您没有权限进入本页!');history.back();</script>");
Response.End();
}
}
else
{
FormsAuthentication.SignOut();
Session.Clear();
Session.Abandon();
Response.Clear();
Response.Write("<script defer>window.alert('您没有权限进入本页或当前登录用户已过期!http://www.cnblogs.com/hsapphire/admin/file://n/请重新登录或与管理员联系!');parent.location='" + virtualPath + "/" + loginPage + "';</script>");
Response.End();
}
}
protected void Page_Error(object sender, System.EventArgs e)
{
string errMsg = "";
Exception currentError = Server.GetLastError();
errMsg += "系统发生错误:<br/>" +
"错误地址: " + Request.Url.ToString() + "<br/>" +
"错误信息: " + currentError.Message.ToString() + "<br/>";
Response.Write(errMsg);
Server.ClearError();//要注意这句代码的使用,清除异常。
}
#endregion
#region URL参数
public virtual string Name
{
get
{
if ((Request["name"] != null) && (Request["name"].ToString() != ""))
{
return Request.QueryString["name"].Trim();
}
return "";
}
}
#endregion
}
}
三。
using System;
using System.Collections.Generic;
using System.Text;
using Permission.Common;
using System.Security.Cryptography;
namespace Permission.WebAdmin
{
/// <summary>
/// 当前用户的标识对象
/// </summary>
[Serializable]
public class SiteIdentity : System.Security.Principal.IIdentity
{
#region 用户属性
private string userName;
private string trueName;
private string email;
private byte[] password;
private int userID;
private string sex;
/// <summary>
/// 用户名
/// </summary>
public string UserName
{
get
{
return userName;
}
}
/// <summary>
/// 真实姓名
/// </summary>
public string TrueName
{
get
{
return trueName;
}
}
/// <summary>
/// 邮箱
/// </summary>
public string Email
{
get
{
return email;
}
}
/// <summary>
/// 用户编号
/// </summary>
public int UserID
{
get
{
return userID;
}
}
/// <summary>
/// 密码
/// </summary>
public byte[] Password
{
get
{
return password;
}
}
/// <summary>
/// 性别
/// </summary>
public string Sex
{
get
{
return sex;
}
}
#endregion
#region IIdentity interface requirments:
/// <summary>
/// 当前用户的名称
/// </summary>
public string Name
{
get
{
return userName;
}
}
/// <summary>
/// 获取所使用的身份验证的类型。
/// </summary>
public string AuthenticationType
{
get
{
return "Custom Authentication";
}
set
{
// do nothing
}
}
/// <summary>
/// 是否验证了用户
/// </summary>
public bool IsAuthenticated
{
get
{
return true;
}
}
#endregion
/// <summary>
/// 根据用户名构造
/// </summary>
public SiteIdentity(string currentUserName)
{
Tb_Accounts_Users entityUser=BllAccess . UserDLL.UserGetModelByUserName(currentUserName);
userName = currentUserName;
trueName = entityUser.TrueName;
email = entityUser.Email;
userID = entityUser.UserID;
password = entityUser.Password;
sex = entityUser.Sex;
}
/// <summary>
/// 根据用户ID构造
/// </summary>
public SiteIdentity(int currentUserID)
{
Tb_Accounts_Users entityUser = UserDLL.UserGetModelByUserID(currentUserID);
userName = entityUser.UserName;
trueName = entityUser.TrueName;
email = entityUser.Email;
userID = currentUserID;
password = entityUser.Password;
sex = entityUser.Sex;
}
/// <summary>
/// 检查当前用户对象密码
/// </summary>
public int TestPassword(string password)
{
// At some point, we may have a more complex way of encrypting or storing the passwords
// so by supplying this procedure, we can simply replace its contents to move password
// comparison to the database (as we've done below) or somewhere else (e.g. another
// web service, etc).
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] hashBytes = encoding.GetBytes(password);
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] cryptPassword = sha1.ComputeHash(hashBytes);
return UserDLL.TestPassword(userID, cryptPassword);
}
}
}
相关文章推荐
- 支持MSNP15的MSN登录程序PYTHON版代码
- 请你不要侮辱我的劳动成果侮辱我的程序代码,我不是传说中的菜鸟,请你不要对我进行人生攻击。
- 编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误,可以重新输入,最多输入三次。三次均错,则提示退出程序
- 请你不要侮辱我的劳动成果侮辱我的程序代码,我不是传说中的菜鸟,请你不要对我进行人生攻击。
- 请你不要侮辱我的劳动成果侮辱我的程序代码,我不是传说中的菜鸟,请你不要对我进行人身攻击。
- 网上代码研究后修改的PHP登录程序,关闭浏览器session清空,可以设置cookies时间
- 网上代码研究后修改的PHP登录程序,关闭浏览器session清空,可以设置cookies时间
- java代码实现网页程序自动登录
- 编写代码实现,模拟用户登录情景,并且只能登录三次,只允许输入三次密码,如果密码正确则提示登录成功,如果三次均输入错误,则退出程序。
- 编写代码模拟三次密码输入的场景。最多能输入三次密码,密码正确,提示“登录成功”, 密码错误,可以重新输入,最多输入三次。三次均错,则提示退出程序。
- php实现简单用户登录功能程序代码
- 网上代码研究后修改的PHP登录程序,关闭浏览器session清空,可以设置cookies时间
- [python] HDU自动登录提交代码程序
- 编写代码实现,模拟用户登录情景,并且只能登录三次。 只允许输入三次密码,如果密码正确则提示登录成,如果三次均输入错误,则退出程序。
- 几行代码编写一键登录和退出QQ程序
- 网上代码研究后修改的PHP登录程序,关闭浏览器session清空,可以设置cookies时间
- 编写高质量代码改善C#程序的157个建议——建议64:为循环增加Tester-Doer模式而不是将try-catch置于循环内
- java实现微信小程序登录态维护的示例代码
- 程序读取需要登录网页的C#代码
- 程序读取需要登录网页的C#代码