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