c# – 如何在不参考System.Web的情况下检查Web.Config中的身份验
发布时间:2020-12-15 07:41:49 所属栏目:百科 来源:网络整理
导读:我有一个类需要从web.config检查身份验证模式. 例如: authentication mode="Forms" / 要么 authentication mode="Windows" / 现在,我知道可以使用以下代码轻松完成此操作: AuthenticationSection sec = ConfigurationManager.GetSection("system.web/authe
我有一个类需要从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).如果我没弄错,客户端配置文件不包含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") (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |