Adam Plocher
Adam Plocher

Reputation: 81

How to check Authentication Mode value in Web.Config without referencing System.Web

I have a class that needs to check the Authentication Mode from a web.config.

Ex:

<authentication mode="Forms" />

or

<authentication mode="Windows" />

Now, I know this can be done pretty easily with the following code:

AuthenticationSection sec = ConfigurationManager.GetSection("system.web/authentication");
if (sec.Mode == "Windows")
{ ... }

My problem is, this class/project is being referenced in my Web project, as well as a WinForms project. The WinForms project is requiring .NET 4.0 Client Profile Framework (we don't want to require the full .NET 4 Framework, if possible). If I'm not mistaken, the Client Profile does not contain System.Web.dll.

Is there a way that this value can be checked without referencing System.Web (and preferably without manually parsing the config file)?

I've tried:

object authSection = ConfigurationManager.GetSection("system.web/authentication");
if (authSection.ToString() == "Windows")
{ ... }

However the ToString() simply returns the string "System.Web.Configuration.AuthenticationSection".

Thank you!

Upvotes: 8

Views: 5389

Answers (3)

Anitha
Anitha

Reputation: 61

I have used the above code to get the authentication mode. I just done few changes in your code. Please find here.

AuthenticationSection authSection = (AuthenticationSection)ConfigurationManager.GetSection("system.web/authentication"); 
if (authSection.Mode.ToString() == "Windows")  

Upvotes: 6

blowdart
blowdart

Reputation: 56500

Where in your code do you need to make a decision on this? If the user is authenticated at that point you could use IIdentity.AuthenticationType and process accordingly. For Forms this will always return Forms, for a Windows identity it typically NTLM, although it can be Negotiate or Kerberos.

Upvotes: 0

Terrance
Terrance

Reputation: 11872

Hey if your talking about a web config in the same project try using the following method.

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel)

Or you could use one of the other similar methods in the ConfigurationManager members. I can't test it for you at the moment but I'm pretty sure they should work. Because essentially they don't care what kind of conf file it is as long as there is one as since the inherited type of the web.config is a config, you should be able to access it just like any other and query for the particular field you need.

ConfigurationManager

Upvotes: 0

Related Questions