2

我试图确保任何 URL 的任何请求都会调用表单身份验证模块,并重定向到登录页面。这目前适用于任何具有授权属性的控制器,但不适用于诸如 /Content/site.css 或任何其他 /[Page] 之类的东西

我按照此处的说明进行操作,但没有成功。

总结是,根据我在网上搜索的所有内容,我最终需要从 Web.config 中“modules”元素下的“FormsAuthentication”模块条目中删除“managedHandler”前置条件。我删除了它(以及其他几个仅用于测试的模块,无济于事)。此外,我不使用 Web.config 中的“授权”元素,因为这不是 MVC 应用程序的最佳实践,因此我不考虑授权或不授权特定文件路径。

<modules> 
    <remove name="FormsAuthentication" />    
    <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" /> 
    <remove name="FormsAuthenticationModule" />    
    <add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />    
    <remove name="UrlAuthorization" />    
    <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />    
    <remove name="DefaultAuthentication" />    
    <add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" />    
</modules> 

有什么想法吗?

谢谢你。

4

1 回答 1

0

首先,您可以通过Autorize这种方式注册为全局过滤器,您的应用程序中的所有操作都需要身份验证和授权(AllowAnonymous对您希望非授权用户可用的操作使用属性):

public class FilterConfig
{

    public static void RegisterGlobalFilters(GlobalFilterCollection filters, Container container)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new System.Web.Mvc.AuthorizeAttribute());                  
    }
}

据我所知,您不需要modules为此问题修改配置中的任何内容

现在关于静态内容:

您可以尝试对包含静态文件的文件夹使用位置设置:

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

PS您必须将所有公共静态内容(公共css文件和脚本)提取到其他文件夹,因为内容和脚本需要身份验证。

于 2015-07-16T06:46:36.297 回答