C# 检测 Web.Config 身份验证模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/91831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting Web.Config Authentication Mode
提问by GateKiller
Say I have the following web.config:
假设我有以下 web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authentication mode="Windows"></authentication>
</system.web>
</configuration>
Using ASP.NET C#, how can I detect the Mode value of the Authentication tag?
使用 ASP.NET C#,如何检测 Authentication 标记的 Mode 值?
采纳答案by redsquare
Try Context.User.Identity.AuthenticationType
尝试 Context.User.Identity.AuthenticationType
Go for PB's answer folks
去寻求 PB 的答案吧
回答by Paul van Brenk
The mode property from the authenticationsection: AuthenticationSection.Mode Property (System.Web.Configuration). And you can even modify it.
来自 authenticationsection 的模式属性:AuthenticationSection.Mode Property (System.Web.Configuration)。你甚至可以修改它。
// Get the current Mode property.
AuthenticationMode currentMode =
authenticationSection.Mode;
// Set the Mode property to Windows.
authenticationSection.Mode =
AuthenticationMode.Windows;
This article describes how to get a reference to the AuthenticationSection.
回答by timvw
use an xpath query //configuration/system.web/authentication[mode] ?
使用 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;
}
回答by bkaid
Import the System.Web.Configurationnamespace and do something like:
导入System.Web.Configuration命名空间并执行以下操作:
var configuration = WebConfigurationManager.OpenWebConfiguration("/");
var authenticationSection = (AuthenticationSection)configuration.GetSection("system.web/authentication");
if (authenticationSection.Mode == AuthenticationMode.Forms)
{
//do something
}
回答by clD
You can also get the authentication mode by using the static ConfigurationManagerclass to get the section and then the enum AuthenticationMode.
您还可以通过使用静态ConfigurationManager类获取部分然后获取 enum 来获取身份验证模式 AuthenticationMode。
AuthenticationMode authMode = ((AuthenticationSection) ConfigurationManager.GetSection("system.web/authentication")).Mode;
The difference between WebConfigurationManager and ConfigurationManager
WebConfigurationManager 和 ConfigurationManager 的区别
If you want to retrieve the name of the constant in the specified enumeration you can do this by using the Enum.GetName(Type, Object)method
如果要检索指定枚举中常量的名称,可以使用以下Enum.GetName(Type, Object)方法执行此操作
Enum.GetName(typeof(AuthenticationMode), authMode); // e.g. "Windows"

