利用ASP.NET MVC2進行網站驗證

來源:互聯網
上載者:User

在最近的項目中需要對業主的網站進行時間限制,故研究了一些方法,在此共用下,希望能在大家做同類事情的時候給一個思路。

 

  註冊表許可權,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,代碼如下: 

 

代碼

 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     }

   但是,如果沒有用戶端軟體只是一個網站的話,使用者可以直接輸入其他URL繞過驗證,這樣我們就需要用到Filter。

 

三、ASP.NET MVC2 Filter頁面過濾

  建立一個類繼承ActionFilterAttribute類,覆寫OnActionExecuting方法,代碼如下

代碼

 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             ......              }
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     }

 

 

這樣就能對相關資訊過濾,並指向對應的view上.

 

轉載時,請註明本文來源:www.cnblogs.com/tmywu

 

作者郵箱:tommywu23@gmail.com 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.