在最近的项目中需要对业主的网站进行时间限制,故研究了一些方法,在此共享下,希望能在大家做同类事情的时候给一个思路。
注册表权限,ASP.NET MVC2 Controller验证,ASP.NET MVC2 Filter页面过滤
保存时间信息的文件可以是注册表、XML、其他类型文件;本案列用的是在注册表读写;
一、注册表权限
注册表读写需要注意权限的问题,没有权限会引出System.IOException异常;
解决方案:在注册表中增加权限,regedit进入注册表,右击【项】选择【权限】
win7环境下
添加IIS_IUSRS用户,赋予读写权限(windows2003环境下添加MO2用户)
这样IIS就能取得注册表的读写权限了。
二、ASP.NET MVC2 Controller验证
编写一个Controller(客户端软件请求服务端时候首先先进此Controller进行验证),读取注册表时间信息后与服务器时间进行比较,False转至错误信息View,代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class AuthActionController : Controller
2 {
3 public ActionResult Index()
4 {
5 month = RegEdit.Instance.GetRegValue("Month").ToString().Replace("0", "").Trim();
6 if (!String.IsNullOrEmpty(month)) {
7 TimeSpan ts = DateTime.Today - baseline;
8 days = ts.Days;
9 data = Convert.ToInt32(RegEdit.Instance.GetRegValue("Data").ToString());
... ...
30 if (data > days || days > Convert.ToInt32(month)) {
31 verification = false;
32 } else {
33 verification = true;
34 }
35 } else {
36 verification = false;
37 }
38
39 if (verification)
40 {
41 exc = RegEdit.Instance.ModifyRegEditData("Data", days.ToString());
42 }
43
44 return Content(verification.ToString());
45 }
46
......
55 }
2 {
3 public ActionResult Index()
4 {
5 month = RegEdit.Instance.GetRegValue("Month").ToString().Replace("0", "").Trim();
6 if (!String.IsNullOrEmpty(month)) {
7 TimeSpan ts = DateTime.Today - baseline;
8 days = ts.Days;
9 data = Convert.ToInt32(RegEdit.Instance.GetRegValue("Data").ToString());
... ...
30 if (data > days || days > Convert.ToInt32(month)) {
31 verification = false;
32 } else {
33 verification = true;
34 }
35 } else {
36 verification = false;
37 }
38
39 if (verification)
40 {
41 exc = RegEdit.Instance.ModifyRegEditData("Data", days.ToString());
42 }
43
44 return Content(verification.ToString());
45 }
46
......
55 }
但是,如果没有客户端软件只是一个网站的话,用户可以直接输入其他URL绕过验证,这样我们就需要用到Filter。
三、ASP.NET MVC2 Filter页面过滤
新建一个类继承ActionFilterAttribute类,覆写OnActionExecuting方法,代码如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public override void OnActionExecuting(ActionExecutingContext filterContext)
2 {
3 base.OnActionExecuting(filterContext);
4 this.month = RegEdit.Instance.GetRegValue("Month").ToString().Replace("0", "").Trim();
5 if (string.IsNullOrEmpty(this.month))
6 {
7 this.verification = false;
8 }
9 else
10 {
11 TimeSpan span = (TimeSpan) (DateTime.Today - this.baseline);
12 this.days = span.Days;
13 this.data = Convert.ToInt32(RegEdit.Instance.GetRegValue("Data").ToString());
14 this.zero = RegEdit.Instance.GetRegValue("Zero").ToString();
15 string zero = this.zero;
16 if ((zero != null) && (zero != "0"))
17 {
18 ......
2 {
3 base.OnActionExecuting(filterContext);
4 this.month = RegEdit.Instance.GetRegValue("Month").ToString().Replace("0", "").Trim();
5 if (string.IsNullOrEmpty(this.month))
6 {
7 this.verification = false;
8 }
9 else
10 {
11 TimeSpan span = (TimeSpan) (DateTime.Today - this.baseline);
12 this.days = span.Days;
13 this.data = Convert.ToInt32(RegEdit.Instance.GetRegValue("Data").ToString());
14 this.zero = RegEdit.Instance.GetRegValue("Zero").ToString();
15 string zero = this.zero;
16 if ((zero != null) && (zero != "0"))
17 {
18 ......
}
38 if ((this.data > this.days) || (this.days > Convert.ToInt32(this.month)))
39 {
40 this.verification = false;
41 }
42 else
43 {
44 this.verification = true;
45 }
46 }
47 if (this.verification)
48 {
49 this.exc = RegEdit.Instance.ModifyRegEditData("Data", this.days.ToString());
50 filterContext.HttpContext.Response.Write(this.exc);
51 }
52 else
53 {
54 this.RedirectToRoute(filterContext, new { controller = "Account", action = "LogOn" });
55 }
56 }
57
58
38 if ((this.data > this.days) || (this.days > Convert.ToInt32(this.month)))
39 {
40 this.verification = false;
41 }
42 else
43 {
44 this.verification = true;
45 }
46 }
47 if (this.verification)
48 {
49 this.exc = RegEdit.Instance.ModifyRegEditData("Data", this.days.ToString());
50 filterContext.HttpContext.Response.Write(this.exc);
51 }
52 else
53 {
54 this.RedirectToRoute(filterContext, new { controller = "Account", action = "LogOn" });
55 }
56 }
57
58
在Controller的ActionResult上加上filter,代码如下:
1 [MyFilter]
2 public ActionResult Index()
3 {
4 base.ViewData["Message"] = "Welcome to ASP.NET MVC!";
5 return base.View();
6 }
2 public ActionResult Index()
3 {
4 base.ViewData["Message"] = "Welcome to ASP.NET MVC!";
5 return base.View();
6 }
这样就能对相关信息过滤,并指向对应的view上.
转载时,请注明本文来源:www.cnblogs.com/tmywu
作者邮箱:tommywu23@gmail.com