2012-03-09 74 views
1

我已经为我的网站设置了FormAuthentication。web.config中的<location>命令是否重要?

我想允许匿名访问登录页面及其资源(js,css,images)。

我已经添加到web.config。那里的订单有关系吗?

<configuration> 
    <configSections> 
    <section name="hibernate-configuration" 
      type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> 
    <section name="log4net" 
      type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 
    </configSections> 
    <appSettings> 
    <add key="webpages:Version" value="1.0.0.0" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <location path="~/Authentication.htm"> 
    <system.web> 
     <authorization> 
     <deny users="*" /> 
     </authorization> 
    </system.web> 
    </location> 
    <location path="~/Resources"> 
    <system.web> 
     <authorization> 
     <deny users="*" /> 
     </authorization> 
    </system.web> 
    </location> 
    <location path="~/js"> 
    <system.web> 
     <authorization> 
     <deny users="*" /> 
     </authorization> 
    </system.web> 
    </location> 
    <location path="~/Images"> 
    <system.web> 
     <authorization> 
     <deny users="*" /> 
     </authorization> 
    </system.web> 
    </location> 
    <location path="~/Controllers"> 
    <system.web> 
     <authorization> 
     <deny users="*" /> 
     </authorization> 
    </system.web> 
    </location> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0"> 
     <assemblies> 
     ..... 
     </assemblies> 
    </compilation> 
    <authentication mode="Forms"> 
     <forms name="Login" loginUrl="~/Authentication.htm" 
      protection="All" path="/" timeout="30" /> 
    </authentication> 
    <authorization> 
     <deny users ="?" /> 
     <allow users = "*" /> 
    </authorization> 

为什么我还添加到路径的验证错误?

Authentication.htm?ReturnUrl=%2fResources%2fScripts%2fjquery-1.7.1.min.js:1Uncaught SyntaxError: Unexpected token < 

Authentication.htm?ReturnUrl=%2fjs%2fCommon.js:1Uncaught SyntaxError: Unexpected token < 

Authentication.htm?ReturnUrl=%2fjs%2fAuthentication.js:1Uncaught SyntaxError: Unexpected token < 

回答

2

你的根目录设置拒绝所有未经验证的用户(?)和位置设置拒绝所有用户(*)。

你可能打算这样做:

<!-- web application root settings --> 
<authorization> 
    <deny users ="?" /> 
</authorization> 

<!-- login and static resources --> 
<location path="~/Images"> 
    <system.web> 
    <authorization> 
     <allow users="*" /> 
    </authorization> 
    </system.web> 
</location> 
+0

它没有帮助。我想允许非授权用户在登录页面(js,css,images)上获取所有相关内容。写完你的配置后,我仍然没有得到Imgaes和脚本。 – 2012-03-09 20:20:38

+0

从Visual Studio运行之前,我已经看到过它,但它在部署到IIS时仍然可以工作。 – jrummell 2012-03-09 20:55:26

+0

问题在于'〜'。我认为这一点更加清楚,但是它失败了相对路径 – 2012-03-09 21:06:45

0

顺序并不重要的,如果你有重复的元素,只有最后一个元素会被考虑在内。