我有一个类需要从 web.config 检查身份验证模式。
例如:
<authentication mode="Forms" />
或
<authentication mode="Windows" />
现在,我知道可以使用以下代码轻松完成此操作:
AuthenticationSection sec = ConfigurationManager.GetSection("system.web/authentication");
if (sec.Mode == "Windows")
{ ... }
我的问题是,在我的 Web 项目以及 WinForms 项目中都引用了此类/项目。 WinForms 项目需要 .NET 4.0 Client Profile Framework(如果可能,我们不想要求完整的 .NET 4 Framework)。如果我没记错的话,Client Profile 不包含 System.Web.dll。
有没有一种方法可以在不引用 System.Web(最好是不手动解析配置文件)的情况下检查这个值?
我试过:
object authSection = ConfigurationManager.GetSection("system.web/authentication");
if (authSection.ToString() == "Windows")
{ ... }
但是 ToString() 只返回字符串“System.Web.Configuration.AuthenticationSection”。
谢谢!
最佳答案
我已经使用上面的代码获取了认证模式。我刚刚对您的代码做了一些改动。请在这里找到。
AuthenticationSection authSection = (AuthenticationSection)ConfigurationManager.GetSection("system.web/authentication");
if (authSection.Mode.ToString() == "Windows")
关于c# - 如何在不引用 System.Web 的情况下检查 Web.Config 中的身份验证模式值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5826362/