【问题标题】:Securing ELMAH in ADFS Claims aware MVC 4 app using elmah.mvc.allowedRoles AppSettings使用 elmah.mvc.allowedRoles AppSettings 在 ADFS 声明感知 MVC 4 应用程序中保护 ELMAH
【发布时间】:2014-02-19 23:52:31
【问题描述】:

ELMAH for MVC 支持以下 appsettings 配置

elmah.mvc.allowedRoles
elmah.mvc.allowedUsers

使用角色/用户保护 elmah 路由路径。显然,它适用于 Windows 或表单身份验证。但我无法使其适用于基于声明的身份验证。

有人有这方面的经验吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 elmah elmah.mvc


    【解决方案1】:

    我在网络配置中这样做

    <elmah>
       <security allowRemoteAccess="true" />
       <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="elmah-sqlserver" applicationName="Eers.Web"/>
    </elmah>
    

    再往下

     <location path="elmah">
        <system.web>
          <authorization>       
            <allow users="*"/>
          </authorization>
        </system.web>
      </location>
      <location path="elmah.axd" inheritInChildApplications="false">
        <system.web>
          <httpHandlers>
            <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
          </httpHandlers>
        </system.web>
        <system.webServer>
          <handlers>
            <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
          </handlers>
        </system.webServer>
      </location>
    

    如果您记下该节点,它就像 MVC 中的任何其他安全性一样工作。但它不适用于索赔。为此,您必须编写一个动作过滤器

      <authorization>       
         <allow users="*"/>
      </authorization>
    

    这是我的动作过滤器

     public class ElmahRequestAuthorizationFilter : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
    
            if (filterContext.IsChildAction) return;
    
            var controller = filterContext.RouteData.Values["controller"] as string;
    
            if (controller != null && controller.ToLowerInvariant() != "elmah") return;
    
            var authenticationComponent = GetAuthenticationInfo() // A method that will return us roles;
    
            var goodRoles = new List<string> {
                "TestRole",
                "ThirdLevelSupport",
                "Administrator"
            };
    
            var roles = authenticationComponent.Roles ?? new List<string>();
    
            var thouShaltPass = roles.Intersect(goodRoles).Any();
    
            if (!thouShaltPass)
            {
                throw new HttpException(404, "Not Found");
            }
    
        }
    }
    

    【讨论】:

    • 如何注册您的 ElmahRequestAuthorizationFilter?过滤器是如何被调用的?你不能把属性放在控制器或动作上,因为 Elmah 是一个模块?
    • 将其添加到 App_Start 中 FilterConfig 的其余过滤器中
    猜你喜欢
    • 2019-05-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 2015-07-21
    • 2014-09-22
    • 2013-12-07
    相关资源
    最近更新 更多