且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Attribute有啥用~MVC中标准的用户登陆验证

更新时间:2022-08-14 20:08:47

你是否需要对一个页面进行登陆或其它权限的验证,你的作法是怎样的,可能在以前的程序开发中,会使用

if .... else  这样的条件判断,遍布在你的程序代码中,而对于.net的程序员来说,有一个福音,那不是Attribute,即"特性"

它的出现,改变了代码设计方式,你再也不需要到处都有的if else了,你只要把它写在一个自定义的Attribute里就可以了,其实.这也是net比其它更友好,更对程序员有吸引力的原因,也是我信仰它的原因.

过去的程序:在一个需要登陆的页面可能你要写成这样

1 if (string.IsNullOrEmpty(session("userid")))
2 
3    response.redirect("/account/logon.aspx");

现在的程序:

1  [UserAuthentication(AuthenticationType.BackgroundLogin)]
2     public partial class SystemController : BackgroundBaseController
3     {
4 
5       // ...
6 
7    }

这样,这个类下面的所有方法对应的页面都将进行登陆的验证,是否是很神奇,呵呵.

以下是用户验证特性的完整代码,供大家参考

 1    /// <summary>
 2     /// 用户验证列举
 3     /// </summary>
 4     public enum AuthenticationType
 5     {
 6         /// <summary>
 7         /// 登录
 8         /// </summary>
 9         Login,
10         /// <summary>
11         /// 后台登陆
12         /// </summary>
13         BackgroundLogin,
14         /// <summary>
15         /// 注册
16         /// </summary>
17         Register,
18     }
19 
20     /// <summary>
21     /// 用户验证过滤器:前台
22     /// </summary>
23     public class UserAuthentication : AuthorizeAttribute
24     {
25         public AuthenticationType Authentication { get; set; }
26         /// <summary>
27         /// 构造函数
28         /// </summary>
29         public UserAuthentication()
30             : this(AuthenticationType.Login) { }
31         public UserAuthentication(AuthenticationType authentication)
32         {
33             this.Authentication = authentication;
34         }
35         /// <summary>
36         /// 执行前验证
37         /// </summary>
38         public override void OnAuthorization(AuthorizationContext filterContext)
39         {
40 
41             //验证不成功的时候
42             switch (this.Authentication)
43             {
44                 case AuthenticationType.Login:
45                     if (!ClientHelper.Current.HasUserInfo)
46                         filterContext.Result = new RedirectResult
47 ("/Account/LogOn?returnUrl=" + HttpContext.Current.Request.Url.ToString());
48                     break;
49 
50                 case AuthenticationType.BackgroundLogin:
51                     if (string.IsNullOrEmpty(SessionAction.ReadSession("Background_Current_UserID")) || Convert.ToInt32(SessionAction.ReadSession("Background_Current_UserID")) < 0)
52                         filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary { { "Action", "LogOn" }, { "Controller", "Account" }, { "returnUrl", HttpContext.Current.Request.Url.ToString() } });
53                     break;
54                 case AuthenticationType.Register:
55                     filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary { { "Action", "Register" }, { "Controller", "Account" } });
56                     break;
57                 default:
58                     filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary { { "Action", "Index" }, { "Controller", "Home" } });
59                     break;
60             }
61 
62         }
63     }

大家请注意一个地方RouteValueDictionary这个类型,可以建立一个字典对象,将你的action和controller写进去就可以组成一个完成的URL,是方法RedirectToRouteResult的作用是叫程序跳到指定的路由中去,你的路由中可以是以SHTML为扩展名,也可以是ASPX,再或者HTML都可以自动选择的.呵呵.

本文转自博客园张占岭(仓储大叔)的博客,原文链接:Attribute有啥用~MVC中标准的用户登陆验证,如需转载请自行联系原博主。