c# - 如何在不引用 System.Web 的情况下检查 Web.Config 中的身份验证模式值

标签 c# .net asp.net

我有一个类需要从 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/

相关文章:

c# - 在 .NET 中获取最近的文档文件夹

asp.net - SQL 网络接口(interface),错误 : 26 - Error Locating Server/Instance Specified

c# - 这真的是一种简化吗?

c# - 实现决策树

c# - 从 Visual Studio 连接到 SQL Server Express

c# - 我们可以在一个表单中有多个提交类型按钮吗?

带有静态文件问题的 ASP.NET VirtualPathProvider

c# - 在表单例份验证中获取当前的 MembershipUser

.net - CodeAccessSecurityEngine 异常

php - 如果 VTiger 使用 PHP,那么哪个 CRM 用于 Rails?