Asp.net MVC学习日记六(过滤黑名单,使其无法访问)

1、Global.asax添加

  routes.MapRoute("BlacklistFilter", "{*path}",new { controller = "Blacklist", action = "Index" },
                new { isBlacklisted = new BlacklistConstraint() }
             );

2、Models文件下新建BlacklistConstraint类,使其继承IRouteConstraint

public class BlacklistConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            bool blacklisted = false;
            //you could 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、新建BlacklistController和对应视图

    public class BlacklistController : Controller
    {
        //
        // GET: /Blacklist/

        public ActionResult Index()
        {
            return View();
        }

    }

4、视图里添加以下代码

    <h2>Uh oh!</h2>
    <div>
        It looks like you have been blacklisted. If you feel that you are seeing this mistakenly
        please contact us at...
    </div>
    <fieldset>
        <legend>Why was I blacklisted?</legend>
        <%= ViewData["reason"]%>

    </fieldset>

 

ok.只是好像页面无法显示ViewData["reason"]的值,我还没想明白为什么

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leesmn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值