How to check whether the application is single sign on or form authentication from C# code?

Suppose you have a C# web application which need to run on a internal (inside network) and external (DMZ) web servers. These two application talks to the same database. The internal application need single sign on (SSO) and the external application is form authentication. Now in Visual studio you have one project for both these applications. Depending on the web.config’s configurations the application either do SSO or form authentication.

For SSO the web.config settings are,
<authentication mode="Windows"></authentication>

For form the web.config settings are,
<authentication mode="Forms">
<forms name="LegalAidLogin" loginUrl="~/login.aspx" defaultUrl="~/default.aspx" timeout="30" protection="All" enableCrossAppRedirects="true">
</forms>
</authentication>

Now, when you in login page which you have to need to check the authentication mode on page load, use the code below,

// Load web.config
System.Xml.XmlDocument config = new System.Xml.XmlDocument();
config.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
System.Xml.XmlNode node = config.SelectSingleNode(”//configuration/system.web/authentication”);

// Check the login mode for forgot your password
if (node.Attributes[“mode”].Value == “Forms”)
{}

If you are already login in and you need to check the authentication mode use code below,

if (HttpContext.Current.User.Identity.AuthenticationType != “Forms”)
{}

Diganta Kumar is an experienced Technical Program Manager with a passion for technology. He has architected and developed software for over a decade for a broad range of industries. Diganta is a founder of two online IT businesses. He likes to help, mentor, and manage software development teams to improve and produce great software. He currently works as a Principal Program Manager for Microsoft. Before joining Microsoft, he was with AWS for five years, where he managed large cross-functional programs on a global scale.

Posted in C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: