在开发过程中,权限验证必不可少。
权限管理模型可以参考我原来的文章,大家可以一起讨论下http://www.cnblogs.com/ttcre2/archive/2008/07/24/1250591.html
在这里要说的是在系统中如何实现权限管理。
我一般是这么做的。
1. 使用Forms认证,我对session是彻底伤心了。
2. 在页面基类中验证当前用户是否已登录。如果登录了,通过cookie加载用户信息。
3. 通过特性来标识一个页面,如果加上了这个特性,就表示这个页面必须登录后才可以访问。
首先配一下web.config,告诉系统登录页和默认页。
<
authentication
mode
="Forms"
>
<
forms
name
="CE.SqlString.SqlStringTest"
loginUrl
="~/Logon.aspx"
defaultUrl
="~/Default.aspx"
/>
</
authentication
>
用户信息的基类。
using
System;
using
System.Collections.Generic;
using
System.Collections.ObjectModel;
using
System.Text;
using
System.Web;
using
System.Web.Security;
using
System.Web.Configuration;
using
System.Configuration.Provider;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
CE.Components.Base.Security
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public abstract class UserInfoBase : ProviderBase
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string userName;
private string data;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected UserInfoBase()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
FormsIdentity identity;
FormsAuthenticationTicket ticket;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//如果应用程序使用forms认证,并且已经登录过了。
if (SystemWebConfig.AuthenticationMode == AuthenticationMode.Forms &&
HttpContext.Current.User.Identity.IsAuthenticated)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//获取cookie中的信息。
identity = (FormsIdentity)HttpContext.Current.User.Identity;
ticket = identity.Ticket;
userName = ticket.Name;
data = ticket.UserData;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected string Data
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return data;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取或设置用户的名称。
/// </summary>
public string UserName
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return userName;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
userName = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。
/// </summary>
/// <param name="userName">经过身份验证的用户名。</param>
/// <param name="createPersistentCookie">若要创建持久 Cookie(跨浏览器会话保存的 Cookie),则为 true;否则为 false。</param>
public static void RedirectFromLoginPage(string userName, bool createPersistentCookie)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (string.IsNullOrEmpty(userName))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("userName");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
FormsAuthentication.RedirectFromLoginPage(userName, createPersistentCookie);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。
/// </summary>
/// <param name="userName">经过身份验证的用户名。</param>
/// <param name="createPersistentCookie">若要创建持久 Cookie(跨浏览器会话保存的 Cookie),则为 true;否则为 false。</param>
/// <param name="userData">存储在票证中的用户特定的数据。</param>
public static void RedirectFromLoginPage(string userName, bool createPersistentCookie, string userData)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
FormsAuthenticationTicket ticket;
string encTicket;
HttpCookie cookie;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (string.IsNullOrEmpty(userName))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("userName");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (string.IsNullOrEmpty(userData))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("userData");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ticket = new FormsAuthenticationTicket
(1, userName, DateTime.Now,
DateTime.Now.AddMinutes(30), //保存多长时间。
createPersistentCookie, userData, FormsAuthentication.FormsCookiePath);
encTicket = FormsAuthentication.Encrypt(ticket);
cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//是否保存cookie。
if (createPersistentCookie)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//过期时间同步。
//cookie和ticket两个当中任何一个时间失效forms就会认证失效。
cookie.Expires = ticket.Expiration;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cookie.HttpOnly = true; //允许客户端脚本访问。
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
HttpContext.Current.Response.Cookies.Add(cookie);
HttpContext.Current.Response.Redirect(FormsAuthentication.GetRedirectUrl(userName, createPersistentCookie));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
再啰嗦两句。在这个类中2个静态方法RedirectFromLoginPage是用来用户登录的。比如你在登录页面验证了帐号和密码,然后就可以通过该方法登录系统了。如果你是其他页面跳过来的,登录成功后就会跳到刚才那个页面。如果你直接访问的登录页,登录成功后就会自动跳转到默认页面(web.config中设置的那个defaultUrl)。
然后在建立一个AuthenticatedAttribute类。这个类用来标识一个页面是否必须登录后才能访问。
这里用了特性去标识页面,而没用微软的,在webconfig里面配置。主要原因是他只能配到目录一级,而不能到页面 级。
using
System;
using
System.Collections.Generic;
using
System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
CE.Components.Base.Security
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 用来标记一个页面或一个控件所在的页面是否需要自动验证是否登录。
/// </summary>
[AttributeUsageAttribute(AttributeTargets.Class)]
public sealed class AuthenticatedAttribute : Attribute
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private bool isAuthenticated;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 构造 AuthenticatedAttribute 类型的新实例。
/// </summary>
public AuthenticatedAttribute()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
isAuthenticated = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 构造 AuthenticatedAttribute 类型的新实例。
/// </summary>
/// <param name="isAuthenticated">该类是否自动验证。true为是。</param>
public AuthenticatedAttribute(bool isAuthenticated)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.isAuthenticated = isAuthenticated;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool IsAuthenticated
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return isAuthenticated;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
做个页面的基类。他来实现权限验证。
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Web.UI;
using
System.Web.Security;
using
System.Web.Configuration;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using
CE.Components.Base.Security;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
CE.Components.Base
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public class PageBase : Page
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private UserInfoBase userInfo;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public PageBase()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//用户登录检查。
Authenticated();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取当前用户信息。
/// </summary>
protected UserInfoBase UserInfo
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return userInfo;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Authenticated()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AuthenticatedAttribute attribute;
string userInfoProviderName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//检查当前页面是否标记自动验证登录的属性。
foreach (Attribute attr in Page.GetType().GetCustomAttributes(true))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
attribute = attr as AuthenticatedAttribute;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//如果标记了。
if (attribute != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (attribute.IsAuthenticated)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//如果当前页面需要自动验证登录,并且应用程序使用forms认证。
if (SystemWebConfig.AuthenticationMode == AuthenticationMode.Forms)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//如果没有登录
if (!User.Identity.IsAuthenticated)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//自动跳转到登录页。
FormsAuthentication.RedirectToLoginPage();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
else //如果登录了,加载用户信息。
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//UserInfoBase userInfo = new UserInfoBase();
userInfoProviderName =
WebConfigurationManager.AppSettings["UserInfoProviderName"];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//如果设置用户信息提供类型。
if (!string.IsNullOrEmpty(userInfoProviderName))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
userInfo = ProviderFactory.LoadProvider
(userInfoProviderName) as UserInfoBase;
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
break;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
这个类其实很简单,但不得不提一下ProviderFactory。
这里需要配下webconfig的appsetting节点。
<add key="UserInfoProviderName" value="UserInfoProvider" />
可以参考http://www.cnblogs.com/ttcre2/archive/2008/07/28/1254671.html
他是微软做的,我就只封装了下,我觉得挺好的,起码比一般的反射要好。
基类都做完了,然后看下调用吧。
首先是用户信息。
using
System;
using
System.Collections.Generic;
using
System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
CE.Components.Base.Security
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public class UserInfo : UserInfoBase
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public UserInfo()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//this.Data 是直接从cookie里面读出来的。
//你可以在登录的时候把些常用的信息序列化成字符串存到data中。
//像部门,职位,员工ID,邮箱等等。
//然后在这里得到data,反序列化这个字符串,还原成属性。
//当然,你也可以只记个id,然后在这里自己去数据库查。很灵活,随便你。
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
然后是登录方法
protected
void
Login1_LoggingIn(
object
sender, LoginCancelEventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string userData;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//先到数据库中去验证。顺便取得需要放入cookie的信息。
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//然后登录。
userData = "用户信息,序列化后的字符串";
UserInfoBase.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet, userData);
}
最后做个需要做权限验证页面的基类继承PageBase,给这个基类加上特性Authenticated(true)。那么只要集成了这个类的页面就会自动验证用户和加载用户信息了。未登录的直接跳到登录页,登录后返回原来页面。多方便啊。
总结:
以上方法只需要给页面加上一个特性后,页面就会自动具备身份验证功能。
身份验证成功后,自动加载用户信息。
用户信息格式完全由开发人员自定义。
加这篇已经是第三篇页面基类的文章了,以后还会陆续加。我这里还有好多,都是平时工作中碰到的问题。有个好的框架,开发起来但速度快,更重要的是质量。