It is easy to restrict access to aspx pages, just use role-checking logic in the code-behind. But resource files like a photo does not have a code behind to put role-checking logic, so how to restrict access?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First, you will need to set up IIS. If you have IIS7+, it’s a snap. Change your app pool from Classic to Integrated Pipeline. This allows managed modules and handlers to be applied to your static resource files. If you are using IIS6, see this article.
Second, you may need to ensure this setting in your web.config (for IIS7):
Things like FormsAuth should now work the same as they would for ASPX, etc., meaning you can restrict paths to authorized users only by using web.config (for example).
Update
In response to Aperture’s comment below:
Outside of using
RoleProviders, ASP.NET can figure out the roles for a principal either by reading groups a user belongs to when using Windows auth, or manually changing the roles by replacing the current IPrincipal in your application, preferably duringAuthenticateRequest.Global.asax.csNow, as far as checking the roles we’ve specified above, there are a number of ways. You could create a custom HttpModule that looks for paths that end in JPG, GIF, JS, etc. and then simply check
context.User.IsInRole. You could also simply uselocationandauthorizationin your web.config:The bottom line is, you can’t execute any managed code during the request to static resources until you either configure Integrated Pipeline, or map static resources to the ASP.NET ISAPI module. So, my answer is appropriate.