简体   繁体   English

如何在不参考 System.Web 的情况下检查 Web.Config 中的 Authentication Mode 值

[英]How to check Authentication Mode value in Web.Config without referencing System.Web

I have a class that needs to check the Authentication Mode from a web.config.我有一个 class 需要从 web.config 检查身份验证模式。

Ex:前任:

<authentication mode="Forms" />

or或者

<authentication mode="Windows" />

Now, I know this can be done pretty easily with the following code:现在,我知道这可以通过以下代码轻松完成:

AuthenticationSection sec = ConfigurationManager.GetSection("system.web/authentication");
if (sec.Mode == "Windows")
{ ... }

My problem is, this class/project is being referenced in my Web project, as well as a WinForms project.我的问题是,这个类/项目在我的 Web 项目以及 WinForms 项目中被引用。 The WinForms project is requiring .NET 4.0 Client Profile Framework (we don't want to require the full .NET 4 Framework, if possible). WinForms 项目需要 .NET 4.0 客户端配置文件框架(如果可能,我们不希望需要完整的 .NET 4 框架)。 If I'm not mistaken, the Client Profile does not contain System.Web.dll.如果我没记错的话,客户端配置文件不包含 System.Web.dll。

Is there a way that this value can be checked without referencing System.Web (and preferably without manually parsing the config file)?有没有一种方法可以在不引用 System.Web 的情况下检查该值(最好不手动解析配置文件)?

I've tried:我试过了:

object authSection = ConfigurationManager.GetSection("system.web/authentication");
if (authSection.ToString() == "Windows")
{ ... }

However the ToString() simply returns the string "System.Web.Configuration.AuthenticationSection".但是 ToString() 只返回字符串“System.Web.Configuration.AuthenticationSection”。

Thank you!谢谢!

I have used the above code to get the authentication mode.我已经使用上面的代码来获取身份验证模式。 I just done few changes in your code.我刚刚对您的代码进行了一些更改。 Please find here.请在这里找到。

AuthenticationSection authSection = (AuthenticationSection)ConfigurationManager.GetSection("system.web/authentication"); 
if (authSection.Mode.ToString() == "Windows")  

Hey if your talking about a web config in the same project try using the following method.嘿,如果您在同一项目中谈论 web 配置,请尝试使用以下方法。

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel)

Or you could use one of the other similar methods in the ConfigurationManager members. 或者,您可以使用 ConfigurationManager 成员中的其他类似方法之一。 I can't test it for you at the moment but I'm pretty sure they should work. 我目前无法为您测试它,但我很确定它们应该可以工作。 Because essentially they don't care what kind of conf file it is as long as there is one as since the inherited type of the web.config is a config, you should be able to access it just like any other and query for the particular field you need. 因为基本上他们不关心它是什么类型的 conf 文件,只要有一个,因为 web.config 的继承类型是一个配置,你应该能够像任何其他配置一样访问它并查询特定的你需要的领域。

ConfigurationManager 配置管理器

Where in your code do you need to make a decision on this?您需要在代码中的哪个位置对此做出决定? If the user is authenticated at that point you could use IIdentity.AuthenticationType and process accordingly.如果此时用户已通过身份验证,您可以使用IIdentity.AuthenticationType并进行相应处理。 For Forms this will always return Forms, for a Windows identity it typically NTLM, although it can be Negotiate or Kerberos.对于 Forms,这将始终返回 Forms,对于 Windows 身份,它通常是 NTLM,尽管它可以是协商或 Kerberos。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM