Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

asp.net

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!

like image 526
Adam Plocher Avatar asked Apr 28 '11 23:04

Adam Plocher


1 Answers

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")  
like image 144
Anitha Avatar answered Sep 27 '22 21:09

Anitha