3

I am trying to restrict users(except admin) to access my folder images. For example the path is:

~/content/images/coverBeg.jpg

If the user navigates to domain/content/images/coverBeg.jpg, he can see the file. I've tryied different sort of things but none of them worked for me. In web config file i've added :

    <location path="~/content/images">
    <system.web>
      <authorization>
        <allow roles="Admin"/>
        <deny users ="*" />
      </authorization>
    </system.web>
  </location>

With no success. After that i've added a web config file to images folder and add those lines of code :

<?xml version="1.0"?>
<configuration>

    <system.web>
      <authorization>
        <allow roles="Admin"/>
        <deny users ="*" />
      </authorization>
    </system.web>

</configuration>

Neither this worked for me. Still everyone can access coverBeg.jpg file

1 Answer 1

3

It's because static content, like images, are served directly by IIS, not involving MVC pipeline. To change that, you can do the following:

add

<modules runAllManagedModulesForAllRequests="true">

to <system.webServer> section of site's web.config. It will run MVC pipeline for every request, including static files - like css, js and images.

Then your config from above will work (I mean your 2nd approach).

Sign up to request clarification or add additional context in comments.

2 Comments

Does it safe approach? There are files where users should pay to access, is it safe?
@ucnobiucnobi as safe as all the rest authentication/authorization mechanism. Basically, what it does - it says IIS to send there requests to MVC, and then standard MVC approach work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.