سؤال

لنفترض أن لدي web.config التالي:

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

باستخدام ASP.NET C#، كيف يمكنني اكتشاف قيمة الوضع لعلامة المصادقة؟

هل كانت مفيدة؟

المحلول

يحاول Context.User.Identity.AuthenticationType

اذهب للحصول على إجابة PB يا جماعة

نصائح أخرى

خاصية الوضع من قسم المصادقة: خاصية AuthenticationSection.Mode (System.Web.Configuration).ويمكنك حتى تعديله.

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

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

توضح هذه المقالة كيفية الحصول على إشارة إلى قسم المصادقة.

استيراد System.Web.Configuration مساحة الاسم وافعل شيئًا مثل:

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

يمكنك أيضًا الحصول على وضع المصادقة باستخدام الوضع الثابت ConfigurationManager class للحصول على القسم ثم التعداد AuthenticationMode.

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

الفرق بين WebConfigurationManager وConfigurationManager


إذا كنت تريد استرداد اسم الثابت في التعداد المحدد، فيمكنك القيام بذلك باستخدام الأمر Enum.GetName(Type, Object) طريقة

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

استخدم استعلام 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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top