Using ASP.NET Forms Authentication, how do I get a

2020-05-21 04:43发布

问题:

I am doing simple forms authentication for a small ASP.NET (3.5, C#) application and setting up my usernames and passwords in the web.config.

I would like to apply the default stylesheet and include the header graphic (included on every other page) but the graphic and stylesheet won't apply, presumably because the anonymous user doesn't have access to those two files. Is there some easy way for me to add them or some other way to make the image appear on the page?

Here is the relevent section of the web.config:

<authentication mode="Forms">
  <forms name=".ASPXFORMSAUTH" 
    path="/" 
    loginUrl="login.aspx" 
    protection="All" timeout="30">

    <credentials passwordFormat="SHA1">
      <user
          name="testuser"
          password="hashgoeshere"/>
    </credentials>
  </forms>
</authentication>
<authorization>
  <deny users="?" />
</authorization>   

The stylesheet is at: /stylesheet.css and the image is at: /img/logoimage.png

Thanks. This site makes me happy because hopefully it will make Experts Exchange and their lame paywall DIE!

回答1:

You can add exceptions in your Web.Config using location-specific rules (add these after the System.Web section):

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

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