Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当web.config设置为Forms时,AuthenticationSection.Mode返回Windows_C#_Asp.net_Asp.net Mvc_Web Config - Fatal编程技术网

C# 当web.config设置为Forms时,AuthenticationSection.Mode返回Windows

C# 当web.config设置为Forms时,AuthenticationSection.Mode返回Windows,c#,asp.net,asp.net-mvc,web-config,C#,Asp.net,Asp.net Mvc,Web Config,因此,我的Web.Config文件具有: <authentication mode="Forms"> <forms loginUrl="~/Home/Index" timeout="2880" /> </authentication> 问题是,无论我在web.config文件中设置了什么值,AuthenticationType最终都是Windows。我需要拉取此值以根据配置方式对页面进行不同的处理,并且似乎无法获得正确的值。我认为将nul

因此,我的Web.Config文件具有:

<authentication mode="Forms">
      <forms loginUrl="~/Home/Index" timeout="2880" />
    </authentication>

问题是,无论我在web.config文件中设置了什么值,AuthenticationType最终都是Windows。我需要拉取此值以根据配置方式对页面进行不同的处理,并且似乎无法获得正确的值。

我认为将null传递给
OpenWebConfiguration
的参数会使其打开机器的配置文件

如果您阅读了有关此的MSDN文档。您会注意到它说传递null将得到根web.config

所以你可能认为这就是你想要的。但事实并非如此。根web.config实际上位于.NET安装路径中。。。。 通常为c:\windows\Microsoft.NET\Framework[.NET版本]\Config

尝试传递配置文件的路径。使用此语句代替path获取当前网站路径

WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath)

这可以确保您在任何环境中每次都能获得正确的配置文件,或者只需使用static
ConfigurationManager.GetSection
方法即可,该方法将在执行代码时为正在运行的应用程序打开config.file

var authentication = (AuthenticationSection)ConfigurationManager.GetSection("system.web/authentication");
AuthenticationType = authentication.Mode;


为当前应用程序的默认配置检索指定的配置节。

可能是它引用了错误的web.config。以下是您可能想要尝试的内容:

 Configuration webconfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
        System.Web.Configuration.SystemWebSectionGroup sysweb = (System.Web.Configuration.SystemWebSectionGroup)webconfig.GetSectionGroup("system.web");
        System.Web.Configuration.AuthenticationSection authSection = sysweb.Authentication;
        System.Web.Configuration.AuthenticationMode authmode = authSection.Mode;

通过检查WindowsPrincipal的IPrincipal类型+1,解决了此问题;今天早上这对我很有帮助。当以非管理员身份运行时,我看到VS下的
OpenWebConfiguration
出现异常。如果您想要顶级的
web.config
,这是正确的选择,在以普通用户身份运行VS时也能正常启动。很抱歉,我要删除一篇旧文章,但我也遇到了类似的问题。此解决方案会引发无法映射“/”的错误。请注意,我尚未部署到IIS。如果是这样的话,我可以进去确保根目录下有一个应用程序。但是,这是在VS2010测试服务器设置中发生的,我没有这样的能力。
 Configuration webconfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
        System.Web.Configuration.SystemWebSectionGroup sysweb = (System.Web.Configuration.SystemWebSectionGroup)webconfig.GetSectionGroup("system.web");
        System.Web.Configuration.AuthenticationSection authSection = sysweb.Authentication;
        System.Web.Configuration.AuthenticationMode authmode = authSection.Mode;