开发者

.NET WebService Custom Attribute

开发者 https://www.devze.com 2022-12-28 11:02 出处:网络
I would like to add a custom attribute to a asmx web service to determine if the request is valid based on the client IP address. This is a similar idea to the AuthorizeA开发者_JS百科ttribute in ASP.N

I would like to add a custom attribute to a asmx web service to determine if the request is valid based on the client IP address. This is a similar idea to the AuthorizeA开发者_JS百科ttribute in ASP.NET MVC.

Is there anywhere (e.g. a HTTP module) I can put the code to look at the attribute on the webservice and decide whether to allow the request or not?

In my web.config the handler for asmx is the ScriptHandlerFactory from the System.Web.Extensions dll.

I have already implemented the functionality with a HTTP module and a config file with a list of allowed URLs, but I would prefer to get rid of the config file and just add an attribute to the webservice class.

Thanks


You can add custom attribute for example

class ValidateIP
{
   public ValidateIP(string ip)
   {
      if(!valid)
       throw CustomerException();
   }
}

and all you need is to add attribute to your class

[ValidateIP(ip)]

Full example:

namespace Test
{
    [TriLLi("11")]
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    class TriLLi : Attribute
    {
        public TriLLi(string ip)
        {
            if(!ip.Equals("10"))
                throw new Exception("You are not allowed to acces this method");
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号