14

次の web.config があるとします。

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

ASP.NET C# を使用して、Authentication タグの Mode 値を検出するにはどうすればよいですか?

4

6 に答える 6

28

認証セクションのモード プロパティ: AuthenticationSection.Mode プロパティ (System.Web.Configuration)。また、それを変更することもできます。

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

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

この記事では、 AuthenticationSection への参照を取得する方法について説明します。

于 2008-09-18T11:50:09.737 に答える
11

名前空間をインポートして、次のSystem.Web.Configurationようにします。

var configuration = WebConfigurationManager.OpenWebConfiguration("/");
var authenticationSection = (AuthenticationSection)configuration.GetSection("system.web/authentication");
if (authenticationSection.Mode == AuthenticationMode.Forms)
{
  //do something
}
于 2011-08-14T00:07:59.380 に答える
5

ConfigurationManager静的クラスを使用してセクションを取得してから enum を取得することにより、認証モードを取得することもできます AuthenticationMode

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

WebConfigurationManager と ConfigurationManager の違い


Enum.GetName(Type, Object)指定された列挙で定数の名前を取得する場合は、メソッドを使用してこれを行うことができます

Enum.GetName(typeof(AuthenticationMode), authMode); // e.g. "Windows"
于 2016-09-26T09:12:37.997 に答える
4

試すContext.User.Identity.AuthenticationType

PBの回答者に行く

于 2008-09-18T11:48:38.757 に答える
-3

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;
}
于 2008-09-18T12:03:34.743 に答える