在ASP.NET 中实现不同角色的用户使用不同登录界面的
方法
很多用户在开发ASP.NET 应用程序时都有这样的需求:管理员角色的账户使用管理员的登录界面进行登录,普通用户角色的账户使用普通用户的登录界面进行登录。由于ASP.NET的web.config里只能使用一个authentication mode="Forms" 节点,所以,要实现不同用户采用不同的登录界面,一个办法就是创建一个管理员专用的虚拟目录,并设置为应用程序来实现。下面介绍另外一种采用重定向的办法来解决这个问题。
本文介绍的方法原理是根据登录界面的返回地址进行判断,然后重定向到不同的页面。下面就是实现的详细过程。
1,创建一个网站,在网站里创建Admin文件夹和User文件夹,分别存放admin和普通用户所使用的文件。也可以只设置一个Admin 文件夹。由于本方法采用的判断返回路径的方法,所以,要能从路径中区分出哪些是admin用户使用的文件夹。当然,采用其他的判断方法也是可以的。
2,在网站根目录下分别创建3个登录文件:Login.aspx、UserLogin.aspx和AdminLogin.aspx。其中Login.aspx文件起地址转换的作用,
Login.aspx文件的主要内容:
view plaincopy to clipboardprint?
protected void Page_Load(object sender, EventArgs e) {
String ReturnUrl = Request.QueryString["ReturnUrl"];
if (ReturnUrl == null || ReturnUrl.Equals(String.Empty)) {
//默认情况下,按普通用户进行登录
Response.Redirect("~/UserLogin.aspx");
}
else
{
if (ReturnUrl.ToLower().Contains("/admin/"))
{
Response.Redirect("~/AdminLogin.aspx?ReturnUrl=" + Server.UrlEncode(ReturnUrl));
}
else
{
Response.Redirect("~/UserLogin.aspx?ReturnUrl=" + Server.UrlEncode(ReturnUrl));
}
}
protected void Page_Load(object sender, EventArgs e) {
String ReturnUrl = Request.QueryString["ReturnUrl"];
if (ReturnUrl == null || ReturnUrl.Equals(String.Empty)) {
//默认情况下,按普通用户进行登录
Response.Redirect("~/UserLogin.aspx");
}
else
{
if (ReturnUrl.ToLower().Contains("/admin/"))
{
Response.Redirect("~/AdminLogin.aspx?ReturnUrl=" + Server.UrlEncode(ReturnUrl));
}
else
{
Response.Redirect("~/UserLogin.aspx?ReturnUrl=" + Server.UrlEncode(ReturnUrl));
}
}
在这个文件的代码中,如果ReturnUrl中含有"/admin/",就
重定向到AdminLogin.aspx登录界面;否则,就重定向到UserLogin.aspx 登录界面。
UserLogin.aspx这个文件的内容如下:
view plaincopy to clipboardprint?
<%@ Page Language="C#" %>
孟宪会之多用户登录测试页面
<%@ Page Language="C#" %>
孟宪会之多用户登录测试页面
这个文件将验证信息保存后,返回最初的请求页面。注意:这里连接数据库验证用户名和密码的过程省略过去了。AdminLogin.aspx这个文件的全部内容如下:
view plaincopy to clipboardprint?
<%@ Page Language="C#" %>
孟宪会之多用户登录测试页面
<%@ Page Language="C#" %>
孟宪会之多用户登录测试页面
注意:这里连接数据库验证用户名和密码的过程省略过去了。
3,在Global的AuthenticateRequest 事件(一定要注意:不是AuthorizeRequest 事件)里将角色信息附加到当前用户的上下文中。
view plaincopy to clipboardprint?
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
string cookieName =
FormsAuthentication.FormsCookieName;
HttpCookie authCookie =
Context.Request.Cookies[cookieName];
if (null == authCookie)
{
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket =
FormsAuthentication.Decrypt(authCookie.Value);
}
catch (Exception ex)
{
return;
}
if (null == authTicket)
{
return;
}
FormsIdentity id = new FormsIdentity(authTicket); String[] roles = id.Ticket.UserData.Split('|'); //读出在登录时设置的角色列表。
System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(id, roles); Context.User = principal;//将验证信息附加到当前用户上下文。
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
string cookieName =
FormsAuthentication.FormsCookieName;
HttpCookie authCookie =
Context.Request.Cookies[cookieName];
if (null == authCookie)
{
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket =
FormsAuthentication.Decrypt(authCookie.Value);
}
catch (Exception ex)
{
return;
}
if (null == authTicket)
{
return;
}
FormsIdentity id = new FormsIdentity(authTicket);
String[] roles = id.Ticket.UserData.Split('|'); //读出在登
录时设置的角色列表。
System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(id, roles);
Context.User = principal;//将验证信息附加到当前用户上下文。
}
4,在web.config文件中,允许登录文件的匿名访问,以便在未登录的情况下显示登录界面,注意:如果包含图片、css 等文件,也需要设置这些资源允许匿名访问。
view plaincopy to clipboardprint?
5,这样,当访问admin文件夹下的内容时,会直接转到AdminLogin.aspx界面。在登录之后,就可以在/Admin/文件夹下的页面中使用下面的方法得到当前登录的用户名和所
具有的角色,根据角色来判断当前用户是否有权操作: view plaincopy to clipboardprint?
Response.Write("
当前登录用户= " +
Page.User.Identity.Name);
Response.Write("admin = " +
Page.User.IsInRole("admin"));
Response.Write("reader = " +
Page.User.IsInRole("reader"));
Response.Write("当前登录用户= " +
Page.User.Identity.Name);
Response.Write("admin = " +
Page.User.IsInRole("admin"));
Response.Write("reader = " +
Page.User.IsInRole("reader"));
为了简单起见,可以写一个Admin使用的基类页面,统一在基类页面中进行权限的处理。
本文来自CSDN博客,转载请标明出处: