使用elmah.mvc.allowedRoles AppSettings保护ADFS声明感知MVC 4应用程序中的ELMAH

时间:2014-02-19 23:52:31

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

ELMAH支持appsettings配置后的MVC支持

elmah.mvc.allowedRoles
elmah.mvc.allowedUsers

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

有没有人有这方面的经验?

1 个答案:

答案 0 :(得分: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中的任何其他安全性一样。但它不适用于声明。因为你必须编写一个Action过滤器

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

这是我的Actionfilter

 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");
        }

    }
}