问题
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
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/1508216/mvc-style-filters-on-asmx-web-services-is-it-possible