开发者

AuthenticationSection.Mode returns Windows when web.config is set to Forms

So my Web.Config File has:

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

in my Application_Start() i make the following calls

Configuration configuration = 开发者_如何学编程WebConfigurationManager.OpenWebConfiguration(null);            
        AuthenticationSection authentication = (AuthenticationSection)configuration.GetSection("system.web/authentication");
        AuthenticationType = authentication.Mode;

The problem is that AuthenticationType ends up being Windows no matter what the value i set in the web.config file. I need to pull this value to process the page differently depending on how it is configured and can't seem to get the right values.


I think passing null to parameter of OpenWebConfiguration is making it open the configuration file of the machine.

If you read the MSDN docs on this. You'll notice it says that passing null will give you the root web.config.

So you may think that's what you want. But it's not. The root web.config is actually in the .NET installation path.... usually c:\windows\Microsoft.NET\Framework[.NET VERSION]\Config

Try passing the path of the Configuration file. Use this statement in place of path to get current website path

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

this makes it sure you get the right config file every time , in any environment


Or just use the static ConfigurationManager.GetSection method which will open the config.file for the running application the code is executed it.

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

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.getsection.aspx

Retrieves a specified configuration section for the current application's default configuration.


May be it is referring wrong web.config. Here is something you might want to try:

 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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜