代码改变世界

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

2005-12-08 18:18  Orin  阅读(264)  评论(0编辑  收藏  举报

.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;
}


本文摘自:http://blog.joycode.com/saucer/archive/2005/12/08/68456.aspx