检测Web.Config身份验证模式

Gat*_*ler 12 c# asp.net authentication web-config

说我有以下web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <authentication mode="Windows"></authentication>
    </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

使用ASP.NET C#,如何检测Authentication标签的Mode值?

Pau*_*enk 28

身份验证部分的mode属性:AuthenticationSection.Mode属性(System.Web.Configuration).你甚至可以修改它.

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

// Set the Mode property to Windows.
authenticationSection.Mode = 
    AuthenticationMode.Windows;
Run Code Online (Sandbox Code Playgroud)

本文介绍如何获取AuthenticationSection的引用.


bka*_*aid 11

导入System.Web.Configuration命名空间并执行以下操作:

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


clD*_*clD 5

您还可以通过使用静态ConfigurationManager类获取部分然后获取 enum 来获取身份验证模式 AuthenticationMode

AuthenticationMode authMode = ((AuthenticationSection) ConfigurationManager.GetSection("system.web/authentication")).Mode;
Run Code Online (Sandbox Code Playgroud)

WebConfigurationManager 和 ConfigurationManager 的区别


如果要检索指定枚举中常量的名称,可以使用该Enum.GetName(Type, Object)方法执行此操作

Enum.GetName(typeof(AuthenticationMode), authMode); // e.g. "Windows"
Run Code Online (Sandbox Code Playgroud)


red*_*are 4

尝试Context.User.Identity.AuthenticationType

去找PB的答案吧