如何获取当前ASP.NET应用的认证模式

.NET 1.1里,

using System.Configuration;
using System.Web.Configuration;
using System.Reflection;

public AuthenticationMode GetAuthenticationMode()
{
 object auth = ConfigurationSettings.GetConfig(“system.web/authentication”);
 if (auth!= null)
 {
 //an internal class “System.Web.Configuration.AuthenticationConfig”
 Type t = auth.GetType();
 PropertyInfo pi = t.GetProperty(“Mode”,BindingFlags.Instance|BindingFlags.NonPublic);
 return (AuthenticationMode)pi.GetValue(auth,null);
  }

  return AuthenticationMode.None;
}

.NET 2.0里,

using System.Configuration;
using System.Web.Configuration;

public AuthenticationMode GetAuthenticationMode()
{
        Configuration config = WebConfigurationManager.OpenWebConfiguration(“~”);

        SystemWebSectionGroup swsg = (SystemWebSectionGroup)config.GetSectionGroup(“system.web”);

        AuthenticationSection auth = swsg.Authentication;
 return auth.Mode;
}

打赏作者