if (Page.IsValid)
{
if(Users.Authenticate(txtUsername.Text, txtPassword.Text))//数据库验证方法,代码略
{
//验证后导向初始页
FormsAuthentication.RedirectFromLoginPage( txtUsername.Text, chkRemember.Checked ;
}
}
在此也可以用 FormsAuthentication.SetAuthCookie(email.Text, RememberCheckbox.Checked);该方法不进行页面导向,而是停留在本页,然后由自己选择导向的页面。
3:然后就要用到global.asax文件下的Application_AuthenticateRequest事件了,此事件在每次访问aspx文件都会触发。
在其中加入如下代码,功能见注释:
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (Request.IsAuthenticated == true) //如果验证了用户,则为 true,否则为 false
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
String[] roles;
// 首次登陆,还没有存入角色cookies
if ((Request.Cookies["userlroles"] == null) || (Request.Cookies["userlroles"].Value == ""))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 此时调用方法,访问数据库中的记录获得用户角色,并存入cookies
roles =(String[]) Users.GetRoles(User.Identity.Name).ToArray(typeof(String));
String roleStr = "";
foreach (String role in roles) //一个用户会有多种角色,以一个字符串表示,用;隔开
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
roleStr += role;
roleStr += ";";
}
//创建cookies票据
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, //版本
Context.User.Identity.Name, //登陆时候存入的标识用户的用户名
DateTime.Now, // 发布时间
DateTime.Now.AddHours(1), // 过期时间
false, // 是否持久
roleStr // 角色字符串
);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 加密票剧
String cookieStr = FormsAuthentication.Encrypt(ticket);
//发送到客户端,起名userroles
Response.Cookies["userroles"].Value = cookieStr;//必须加密
Response.Cookies["userroles"].Path = "/";
Response.Cookies["userroles"].Expires = DateTime.Now.AddMinutes(1);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 已存在,读取,解密
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Context.Request.Cookies["userroles"].Value);
//把角色字符添加到list里
ArrayList userRoles = new ArrayList();
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach (String role in ticket.UserData.Split( new char[] {';'} ))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
userRoles.Add(role);
}
roles = (String[]) userRoles.ToArray(typeof(String));
}
// 把此用户的角色存到内存中,可以运用User.IsInRole()方法进行检验用户角色
// 也可以使用实现IPrincipal接口的类,自定义赋值给Context.User
Context.User= new GenericPrincipal(Context.User.Identity, roles);
}
}
4:然后添加读取验证用户是否存在的访问数据库代码,和获得用户角色的代码。
详细码略,这里主要争对三张表写出获得用户角色的存储过程
CREATE PROCEDURE User_GetUserRolesByUsername
(
@Username nvarchar(50)
)
AS
select Roles.RoleName
from Roles
join Users on Users.Username=@Username
join User_Role on User_Role.UserID=Users.UserID
where Roles.RoleID=User_Role.RoleID
GO
至此,我们就完成了一般性的asp.net窗体验证的功能。
下面着重介绍第二种方法,通过数据库读取用户名密码进行验证。
1:首先在数据库里建立三张表: Users(UserID,UserName,UserPwd)---存放用户信息
Roles(RoleID,RoleName)------存放角色名称
User_Role(UserID,RoleID)-----用户和角色的中间表,使头两张表成为多对多关系
2:然后在登陆页面的登陆按钮点击事件中加入如下逻辑
if (Page.IsValid)
{
if(Users.Authenticate(txtUsername.Text, txtPassword.Text))//数据库验证方法,代码略
{
//验证后导向初始页
FormsAuthentication.RedirectFromLoginPage( txtUsername.Text, chkRemember.Checked ;
}
}
在此也可以用 FormsAuthentication.SetAuthCookie(email.Text, RememberCheckbox.Checked);该方法不进行页面导向,而是停留在本页,然后由自己选择导向的页面。
3:然后就要用到global.asax文件下的Application_AuthenticateRequest事件了,此事件在每次访问aspx文件都会触发。
在其中加入如下代码,功能见注释:
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (Request.IsAuthenticated == true) //如果验证了用户,则为 true,否则为 false
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
String[] roles;
// 首次登陆,还没有存入角色cookies
if ((Request.Cookies["userlroles"] == null) || (Request.Cookies["userlroles"].Value == ""))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 此时调用方法,访问数据库中的记录获得用户角色,并存入cookies
roles =(String[]) Users.GetRoles(User.Identity.Name).ToArray(typeof(String));
String roleStr = "";
foreach (String role in roles) //一个用户会有多种角色,以一个字符串表示,用;隔开
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
roleStr += role;
roleStr += ";";
}
//创建cookies票据
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, //版本
Context.User.Identity.Name, //登陆时候存入的标识用户的用户名
DateTime.Now, // 发布时间
DateTime.Now.AddHours(1), // 过期时间
false, // 是否持久
roleStr // 角色字符串
);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 加密票剧
String cookieStr = FormsAuthentication.Encrypt(ticket);
//发送到客户端,起名userroles
Response.Cookies["userroles"].Value = cookieStr;//必须加密
Response.Cookies["userroles"].Path = "/";
Response.Cookies["userroles"].Expires = DateTime.Now.AddMinutes(1);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 已存在,读取,解密
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Context.Request.Cookies["userroles"].Value);
//把角色字符添加到list里
ArrayList userRoles = new ArrayList();
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach (String role in ticket.UserData.Split( new char[] {';'} ))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
userRoles.Add(role);
}
roles = (String[]) userRoles.ToArray(typeof(String));
}
// 把此用户的角色存到内存中,可以运用User.IsInRole()方法进行检验用户角色
// 也可以使用实现IPrincipal接口的类,自定义赋值给Context.User
Context.User= new GenericPrincipal(Context.User.Identity, roles);
}
}
4:然后添加读取验证用户是否存在的访问数据库代码,和获得用户角色的代码。
详细码略,这里主要争对三张表写出获得用户角色的存储过程
CREATE PROCEDURE User_GetUserRolesByUsername
(
@Username nvarchar(50)
)
AS
select Roles.RoleName
from Roles
join Users on Users.Username=@Username
join User_Role on User_Role.UserID=Users.UserID
where Roles.RoleID=User_Role.RoleID
GO
至此,我们就完成了一般性的asp.net窗体验证的功能。