开发者

Unauthenticated users can't see images in website

开发者 https://www.devze.com 2023-01-24 08:20 出处:网络
When I run my website through debug mode in visual studio everything looks great and all the images on the page show up fine.But once I开发者_JS百科 deploy my website to an IIS7 web server (doubt that

When I run my website through debug mode in visual studio everything looks great and all the images on the page show up fine. But once I开发者_JS百科 deploy my website to an IIS7 web server (doubt that other versions would make any difference, but you never know) then users can't see the images on the site until they log in.

The website is an asp.net MVC site and I'm new to MVC, though I do have lots of experience with asp.net forms. It seems that only authenticated users are allowed to access the images folder, and there is an authorization section in my web.config saying that only admins can access the site, so how do I make it so that all users, authenticated or otherwise can view the images?

-- Update --

I tried putting in the configuration block suggested, which from everything I can tell makes perfect sense, but it didn't seem to make a difference. The sample below has Content/Images for the path but before that I tried just Content, since it's OK for everything in there to be accessible. I also tried setting the allowOverride setting to false, which also didn't seem to make a difference.

<location path="Content/Images">
    <system.web>
        <authorization>
            <allow users ="*" />
        </authorization>
    </system.web>
</location>  

--Update 2-- Funny thing is that I don't even see any explicit deny entries in my web.config, just an allow for admin's, and when I went into IIS 7 and used the UI to allow all users access to the Content directory it didn't show any deny's on the list either. But then again, this project works fine when I just debug it from my personal computer, it's only after I deploy it that I have problems...

<authorization>
    <allow roles="admin" />
</authorization>
<authentication mode="Forms">
    <forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>


In your web.config file, after the </system.web> tag, add the following

<location path="images">
     <system.web>
       <authorization>
        <allow users ="*" />
       </authorization>
    </system.web>
</location>

where path="images" is the name of the folder with your images/css.

Update:

If you're using ASP.NET MVC, I would remove the authorization declaration in the web.config file and use the [Authorize] attribute on the controllers that need to be locked down.

You can also specify the roles you want to grant access to using [Authorize("admin")].


By default, the configuration you define in a Web.config file, applies to every subdirectory of its containing folder. If you have a Web.config in the application root, that configuration will propagate to every subdirectory, including those where you have images and Css style sheets. So, if you have in your root Web.config a rule like this one:

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

anonymous users will not be able to access the resources needed to display the page like you would expect it.

To fix it, you need to include another Web.config in every subdirectory that has presentation resources, and apply the rules you want to override. In your case, this should do the work:

<?xml version="1.0"?>
<configuration>
   <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
   </system.web>
</configuration>

If you are using Themes, place a single Web.config file in the theme folder.


Just ran into the same problem. We noticed it on our Login page. The CSS and images weren't loading (they were under the content directory). Adding IUSR to the folder with Read privileges fixed it for us.


Old question but I came across the same problem and found the answer. I had created a new folder for the Membership pages that I wanted logged-in users to have access to, but the images wouldn't show up in them! It turns out that no matter what I tried I couldn't set a relative path to the images from within the Members folder.

The problem was the html image (as far as I could tell anyway, correct me if I'm wrong). I swapped it out for an asp:image with a relative path and now it works great. My new code: asp:image runat="server" id="Logo" ImageUrl="~/Images/logo.jpg"

Notice the ' ~/ ', that wouldn't work for me with I tried: img src="../Images/logo.jpg", img src="~/Images/logo.jpg", img src="/Images/logo.jpg", etc. to no avail.

Hope this answer helps someone solve this problem more quickly in the future.


Try browsing the image file directly and see if that comes up for an anonymous user. In IIS 7, there is a way in which you can authorize anonymous users directly from your UI [which in turn would create the web.config if it doesn't exist].

Folder -> .NET Authorization Rules -> Add Allow Rule


Are you using membership framework. I suspect that you have misconfigured your permission settings in web.config and your images folder is only allowing authorised requests. This can be easily change via web.config. Just remove the folder name from your authorised section and this should solve the problem.

For your reference this should be the section in web.config if you are using membership framework :

 <authorization>
  <deny roles="xyz" />
</authorization>


We came across the same problem in our project and found a solution, although we are not sure about the reasons of this behaviours.

We had a similar scenario, with a folder containing all our png images. Our marketing colleague wanted to rename some of them, so to make it easier for her, we shared (this might be the key) that folder, granting read/writer rights to her.

From this point, the images started behaving as you describe, requiring authorization for being accessed. No unauthorized user could see them any more.

We realized that the problem was related to sharing the folder because it started happening at that point of time. Otherwise we still don't see any relationship among ASP.NET MVP user authorization/login and hard disk user rights. We could have expected an IO Exception or something similar, but not this behaviour.

After checking the folder, and comparing with other folders, we realized that after sharing, the permissions on the folder had slightly changed. Mainly they did not inherit from its parent folder anymore and some permissions such as CREATOR OWNER and MACHINE_NAME\Users had dissapeared (although IUSR and the app pool user were still there).

So, we renamed that folder, created a new one with the same name, and copied the content from the old folder to the new one. The new folder had the permissions inherited from its parent and everything started to work perfectly again.

It solved our problem, but we are still not sure why this all happened this way.

Regards

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号