views:

98

answers:

1

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 AuthorizeAttribute 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

A: 
TriLLi
That's not how attributes work. Firstly, attribute class must extend from `Attribute`. Atttributes themelves are typically used to give some kind of information to/about the method - not to execute methods in the way you describe
Cocowalla
I have forgotten to inherit attribute class but this is how it works... Basically you can get solution working lke this.
TriLLi
Go ahead and try it - the ValidateIP method will not execute, because that is not how attributes work!
Cocowalla
I have created full example, this is working!!?!?,
TriLLi
Hmm.. I take it back, it does seem to work :) I tested it earlier in a Winforms app and it didn't work, but if I test it in a console app it does - although only on methods, not on classes like you have in your example (even if I use AttributeUsage). Any ideas how to get the client's IP address to pass into an attribute though?
Cocowalla
HttpContext.Current.Request.UserHostAddress
TriLLi
This doesn't work for web services though. We have attributes that we have added to web forms and we have added a HTTP module to look at the page attributes, but I don't know where I can add code for a web service.
DownChapel