Détection du mode d'authentification Web.Config
-
01-07-2019 - |
Question
Disons que j'ai le config web.config suivant:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authentication mode="Windows"></authentication>
</system.web>
</configuration>
Avec ASP.NET C #, comment puis-je détecter la valeur Mode de la balise Authentication?
La solution
Essayez Context.User.Identity.AuthenticationType
Allez chercher les gens de la réponse de PB
Autres conseils
La propriété mode de la section authentications: Propriété AuthenticationSection.Mode (System.Web.Configuration) . Et vous pouvez même le modifier.
// Get the current Mode property.
AuthenticationMode currentMode =
authenticationSection.Mode;
// Set the Mode property to Windows.
authenticationSection.Mode =
AuthenticationMode.Windows;
Cet article décrit la comment obtenir une référence à AuthenticationSection .
Importez l'espace de noms System.Web.Configuration
et procédez comme suit:
var configuration = WebConfigurationManager.OpenWebConfiguration("/");
var authenticationSection = (AuthenticationSection)configuration.GetSection("system.web/authentication");
if (authenticationSection.Mode == AuthenticationMode.Forms)
{
//do something
}
Vous pouvez également obtenir le mode d'authentification à l'aide du paramètre statique ConfigurationManager
pour obtenir la section, puis l'énum AuthenticationMode
.
AuthenticationMode authMode = ((AuthenticationSection) ConfigurationManager.GetSection("system.web/authentication")).Mode;
différence entre WebConfigurationManager et ConfigurationManager
Si vous souhaitez récupérer le nom de la constante dans l'énumération spécifiée, vous pouvez le faire en utilisant la méthode Enum.GetName (Type, Object)
Enum.GetName(typeof(AuthenticationMode), authMode); // e.g. "Windows"
utiliser une requête xpath //configuration/system.web/authentication[mode]?
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument config = new XmlDocument();
config.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
XmlNode node = config.SelectSingleNode("//configuration/system.web/authentication");
this.Label1.Text = node.Attributes["mode"].Value;
}