1

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.

Rich

kim3er
  • 6,306
  • 4
  • 41
  • 69

2 Answers2

3

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: http://msdn.microsoft.com/en-us/library/aa719858(VS.71).aspx

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

Alex Duggleby
  • 7,948
  • 5
  • 37
  • 44
  • 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 Oct 02 '09 at 14:23
0

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
  • 15,573
  • 3
  • 34
  • 53