Asp.net MVC learning diary 6 (filter the blacklist to make it inaccessible)

Source: Internet
Author: User

1. Add global. asax

Routes. maproute ("blacklistfilter", "{* path}", new {controller = "Blacklist", Action = "Index "},
New {isblacklisted = new blacklistconstraint ()}
);

2. Create a blacklistconstraint class under the models file to inherit irouteconstraint

Public class blacklistconstraint: irouteconstraint
{
Public bool match (httpcontextbase httpcontext, route, string parametername, routevaluedictionary values, routedirection)
{
Bool blacklisted = false;
// You cocould get your blacklist data from a database
// And cache it when the app starts
// Ip address list
List <string> _ ipblacklist = new list <string> ()
{
"127.0.0.1 ",
"192.168.1.100 ",
"192.168.1.101 ",
"192.168.1.102 ",
"192.168.1.103 ",
"192.168.1.104 ",
"192.168.1.105 ",
"192.168.1.106 ",
"192.168.1.107 ",
"192.168.1.108 ",
"192.168.1.109 ",
"192.168.1.110"
};
// Username list
List <string> _ usernameblacklist = new list <string> ()
{
"Asiemer ",
"Anonymous"
};

// Email list
List <string> _ emailblacklist = new list <string> ()
{
"Asmith@hotmail.com ",
Jsmith@hotmail.com"
};

// Check IP addresses
If (_ ipblacklist. Contains (httpcontext. Request. userhostaddress ))
{

Values. Add ("reason", "you were blacklisted because of your IP address:" + httpcontext. Request. userhostaddress );
Blacklisted = true;
}
// Check usernames
If (httpcontext. profile! = NULL & _ usernameblacklist. Contains (
Httpcontext. profile. username. tolower ()))
{
Values. Add ("reason", "you were blacklisted because of your Username:" + httpcontext. profile. username. tolower ());
Blacklisted = true;
}
// Check email addresses
If (_ emailblacklist. Contains ("Values ['email ']. tostring ()"))
{
Values. Add ("reason", "you were blacklisted because of your email address: Values ['email ']. tostring ()");
Blacklisted = true;
}

// Do some stuff before booting the user
// If (blacklisted)
//{
/// Of, if you don't want to keep them around...

// Httpcontext. response. Redirect ("http://www.peopleofwalmart.com ");
/// Set the status code to access denied
// Httpcontext. response. statuscode = (INT) httpstatuscode. forbidden;
//}
// Return the result
Return blacklisted;
}
}

 

3. Create a New blacklistcontroller and view

Public class blacklistcontroller: Controller
{
//
// Get:/blacklist/

Public actionresult index ()
{
Return view ();
}

}

4. Add the following code to the View:

<H2> uh oh! </H2>
<Div>
It looks like you have been blacklisted. If you feel that you are seeing this mistakenly
Please contact us...
</Div>
<Fieldset>
<Legend> why was I blacklisted? </Legend>
<% = Viewdata ["reason"] %>

</Fieldset>

 

OK. It just seems that the value of viewdata ["reason"] cannot be displayed on the page. I still have no idea why

Related Article

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.