c# – 使用Attribute创建Request IP约束

c# – 使用Attribute创建Request IP约束,第1张

概述我想做以下(伪代码): [InternalOnly]public ActionResult InternalMethod(){ //magic } “InternalOnly”属性适用于在执行任何其他 *** 作之前应检查HttpContext请求IP是否已知值的方法. 我将如何创建这个“InternalOnly”属性? 您可以创建自定义过滤器属性: public class InternalOnly 我想做以下(伪代码):

[InternalOnly]public ActionResult InternalMethod(){ //magic }

“InternalOnly”属性适用于在执行任何其他 *** 作之前应检查httpContext请求IP是否已知值的方法.

我将如何创建这个“InternalOnly”属性?

解决方法 您可以创建自定义过滤器属性:

public class InternalOnly : Filterattribute{    public voID OnAuthorization (AuthorizationContext filterContext)    {        if (!IsIntranet (filterContext.httpContext.Request.UserHostAddress))        {            throw new httpException ((int)httpStatusCode.ForbIDden,"Access forbIDden.");        }    }    private bool IsIntranet (string userIP)    {        // match an internal IP (ex: 127.0.0.1)        return !string.IsNullOrEmpty (userIP) && Regex.IsMatch (userIP,"^127");    }}
总结

以上是内存溢出为你收集整理的c# – 使用Attribute创建Request IP约束全部内容,希望文章能够帮你解决c# – 使用Attribute创建Request IP约束所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1215425.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存