<location path =“something”comparison =“”如何工作?=“”

时间:2017-03-22 20:39:22

标签: .net web-config location comparison authorization

=“”

我试图修复一个使用旧版Windows Identity Foundation的旧版网站存在问题。我遇到的一个问题是,由于它不是MVC站点,我无法对控制器操作进行身份验证。我们的网站刚刚在每个页面上强制进行身份验证,包括所有设置如此的页面...

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

因为我们有

<system.web>
    <authentication mode="None" />
    <authorization>
      <deny users="?" />
    </authorization>

设置,因为WIF正常工作。

因此,我在global.asax上写了一个像这样的巨大垃圾来试图阅读&#39; web.config和divine每个<Location>项的授权级别。

private void Application_AuthenticateRequest(object sender, EventArgs e)
{
    HttpApplication htApp = (HttpApplication)sender;

    Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
    ConfigurationLocationCollection section = config.Locations;

    foreach (ConfigurationLocation location in section)
    {
        Configuration rootConfiguration = location.OpenConfiguration();
        PropertyInformationCollection props = rootConfiguration.GetSection("system.web/authorization").ElementInformation.Properties;

        foreach (PropertyInformation prop in props)
        {
            AuthorizationRuleCollection arc = prop.Value as AuthorizationRuleCollection;
            if (arc == null) continue;

            foreach (AuthorizationRule rule in arc)
            {
                if (!rule.ElementInformation.IsPresent || rule.Users[0] != "*") continue;

                if (htApp.Request.CurrentExecutionFilePathExtension != null)
                {
                    // Clean up the current request name
                    string tmp = htApp.Request.CurrentExecutionFilePath;
                    if (!string.IsNullOrEmpty(htApp.Request.CurrentExecutionFilePathExtension))
                    {
                        tmp = tmp.Replace(htApp.Request.CurrentExecutionFilePathExtension, string.Empty);
                    }

                    if (tmp.StartsWith("/"))
                    {
                        tmp = tmp.Remove(0, 1);
                    }

                    // something has to make sense at some point
                    // figure out how to compare this to 
                    if (tmp.Contains(location.Path) || location.Path.Contains(tmp))
                    {
                        HttpContext.Current.SkipAuthorization = true;
                    }
                }


            }
        }
    }

}

这样可行,但它可能有安全漏洞,而且它有可怕的代码味道。我无法提交它进行代码审查,因为每个人都会笑,并认为我是个白痴。

我需要知道如何通过<location path="something">获取在web.config中确定的布尔值,并且我需要在身份验证操作发生之前找到一些方法在global.asax中执行。< / p>

如何确定我当前网址的路径是否等同于&#39;到web.config中的<location path

修改 我希望this.Context.Request.RequestContext.RouteData能拥有我想要的东西,但它全都是空的。

0 个答案:

没有答案