Is it possible to create MVC style filter attributes (like Authorize or HandleError) that work on ASMX Web Services?

Specifically, I perform custom authentication on a number of my web methods and would like to wrap the code into an Attribute that throws an exception, if the authentication checks fail.


+1  A: 

Since ASMX are also server by the ASP.NET pipeline, you could just use HttpModules, which give you a lot of control on the way in and the way out.

Here's a reference and an example:

If you want to make it very "MVC-like" then you would write a custom http module that check the webservice for attributes such as [Authorize] etc. Since ASP.NET MVC is open source you may just use parts of that as a reference how they check for attributes etc and then build it into your HTTPModule.

HTH Alex

Are you sure about this? At the least, certain events don't fire for a web service. You can't use an `HttpModule` for global exception handling in a web service, as an example.
John Saunders

You can mimic some effects. Remembering that ASMX web services just wrap any public method, you can use things like the PrincipalPermissionAttribute to secure your services.

Wyatt Barnett