Pergunta

Say Eu tenho o seguinte web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <authentication mode="Windows"></authentication>
    </system.web>
</configuration>

Usando ASP.NET C #, como posso detectar o valor Modo do código de autenticação?

Foi útil?

Solução

Tente Context.User.Identity.AuthenticationType

Vá para a gente resposta do PB

Outras dicas

A propriedade modo a partir do authenticationsection: AuthenticationSection.Mode propriedade (System.Web.Configuration) . E você ainda pode modificá-lo.

// Get the current Mode property.
AuthenticationMode currentMode = 
    authenticationSection.Mode;

// Set the Mode property to Windows.
authenticationSection.Mode = 
    AuthenticationMode.Windows;

Este artigo descreve como obter uma referência para o AuthenticationSection .

Importar o namespace System.Web.Configuration e fazer algo como:

var configuration = WebConfigurationManager.OpenWebConfiguration("/");
var authenticationSection = (AuthenticationSection)configuration.GetSection("system.web/authentication");
if (authenticationSection.Mode == AuthenticationMode.Forms)
{
  //do something
}

Você também pode obter o modo de autenticação usando a estática ConfigurationManager classe para obter a seção e, em seguida, o AuthenticationMode enum.

AuthenticationMode authMode = ((AuthenticationSection) ConfigurationManager.GetSection("system.web/authentication")).Mode;

A diferença entre WebConfigurationManager e ConfigurationManager


Se você quiser recuperar o nome da constante na enumeração especificada Você pode fazer isso usando o método Enum.GetName(Type, Object)

Enum.GetName(typeof(AuthenticationMode), authMode); // e.g. "Windows"

usar um XPath consulta //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;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top