Protecting folders in MVC

2019-01-26 13:41发布

I have some files in my Content folder that I don't want a user to be able to download without being authorised. How do I prevent a user from just getting to the file by typing ...Content/{filename} into the address bar?

3条回答
Ridiculous、
2楼-- · 2019-01-26 13:51

Well one way is to have it outside the context of IIS, so instead of having them under C:\inetpub\wwwroot

change it to something like C:\temp\files.

in your DB have a GUID associated with the document name and use the GUID to display the link to the file.

in your controller action you would just accept the GUID, get the filename and then serve the file in your response.

查看更多
我命由我不由天
3楼-- · 2019-01-26 14:05

it doesn't work for me.

<configuration>
    <appSettings>
    ...
    </appSettings>
    <system.web>
    ...
    </system.web>
    <system.webServer>
    ...
    </system.webServer>
    <location path="Content">
        <system.web>
            <authorization>
                <deny users="?" />
            </authorization>
        </system.web>
    </location>
</configuration>

I run my MVC 4.0 application, login and logout, can't access any app page, but still can access file by direct link like

http://localhost:80966/Content/Files/home.jpg
查看更多
祖国的老花朵
4楼-- · 2019-01-26 14:15

There are a couple of possibilities. The first one consists into using the <location> tag in your web.config:

<location path="Content">
    <system.web>
        <authorization>
            <deny users="?" />
        </authorization>
    </system.web>
</location>

Another possibility is to put those files inside a folder where noone can access (like the App_Data folder for example) and then have a controller action that will serve those files which will be decorated with the [Authorize] attribute.

查看更多
登录 后发表回答