Use ASP. NET MVC2 for website Verification

Source: Internet
Author: User

In recent projects, we need to restrict the Time of the website of the owner. Therefore, we have studied some methods and hope to give us some ideas when doing similar things.

 

Registry permission, ASP. NET MVC2 Controller authentication,ASP. NET MVC2 Filter page Filter 

 

The file that saves the time information can beRegistry, XML, and other types of filesIn this case, the column is read and written in the registry;

 

1. Registry Permissions

You need to pay attention to permissions when reading and writing the Registry. If you do not have permissions, System. IOException is thrown;

Solution: add permissions to the Registry. regedit enters the registry, right-click items, and select permissions]

In win7 Environment

  

 

Add IIS_IUSRS users and grant read and write permissions (add MO2 users in windows2003 environment)

In this way, IIS can obtain the Registry's read and write permissions.

 

Ii. ASP. NET MVC2 Controller Verification

Compile a Controller (the Controller is first verified when the client software requests the server), read the Registry time information and compare it with the server time, False to go to the error information View, the Code is as follows:

 

Code

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}

However, if no client software is just a website, you can directly enter other URLs to bypass verification, so we need to use Filter.

 

3. ASP. NET MVC2 Filter page filtering

Create a class that inherits the ActionFilterAttribute class and overwrite the OnActionExecuting method. The Code is as follows:

Code

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

 

 

Add filter to the ActionResult of the Controller. The Code is as follows:

 

1 [MyFilter]
2 public ActionResult Index ()
3 {
4 base. ViewData ["Message"] = "Welcome to ASP. net mvc! ";
5 return base. View ();
6}

 

 

In this way, the related information can be filtered and directed to the corresponding view.

 

When reprinting, please indicate the source of this article: www.cnblogs.com/tmywu

 

Author Email: tommywu23@gmail.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.