Reading AuthorizationSection from web.config provides invalid values

Edit # 2: config.FilePath shows that it is looking at a different file than I expect: "C: \ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ Config \ web.config". I expected it to use web.config in my project. Find out why this is happening.

I have a method in my web API where I am trying to read the values ​​from the authorization section in my web.config. Based on what I found, this should work:

    public AuthorizationSetting GetAuthorizationSettings()
    {
        var config = WebConfigurationManager.OpenWebConfiguration(null);
        var section = config.GetSection("system.web/authorization") as AuthorizationSection;

        foreach (AuthorizationRule rule in section.Rules)
        {
            if (rule.Action.ToString().ToLower() == "allow")
            {
                Debug.WriteLine(rule);
            }
        }

        return new AuthorizationSetting();
    }

And this is the web.config section that contains the authorization information:

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <identity impersonate="true" />
    <authentication mode="Windows" />
    <authorization>
      <allow roles="role1,role2,role3"/>
      <deny users="*"/>
    </authorization>
  </system.web>

, . , , . , ? Action of Allow "*" . , web.config. ?

enter image description here

** ** web.config. web.config( "" ). , , .

+4
1

, Null in path OpenWebConfiguration web.config

%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\Config\

:

The virtual path to the configuration file. If null, the root Web.config file is opened.

, - , . , :

var config = WebConfigurationManager.OpenWebConfiguration("~");
+1

Source: https://habr.com/ru/post/1648050/


All Articles