c# - 如何使用 HttpListener 处理 WindowsImpersonationContext?

标签 c# visual-studio httplistener

这可能是以前以稍微不同的方式提出的,但我还没有完全满意自己。

通过 Visual Studio 2013、Windows 7 和一个小型 C#/.NET 4.0 项目,我使用 HttpListener 构建一个小型 Web 服务器,以便托管一些简单的服务和页面。此处现有项目升级的一部分是开始使用集成 Windows 身份验证。

该项目提供的服务之一是连接到 SQL 数据库的方法 - 以便运行一些预制更新。使用 WindowsImpersonationContext 和正确的 HttpListener 启动参数,我可以轻松地“模拟”经过身份验证的用户(通过 NTLM)。就像这样:

identity = (System.Security.Principal.WindowsIdentity)context.User.Identity;

using (System.Security.Principal.WindowsImpersonationContext wic = identity.Impersonate())
{
    doListenerCallback(context, lentry, true, identity);
}

这个想法是访问网页的用户的凭据用于打开数据库连接。所以这段代码很好,它给了我一个对象,一切看起来都很好。

一切正常直到我使用常规用户帐户(非管理员)运行此项目(它托管在控制台应用程序内)。模拟似乎仍然发生,但我的应用程序域不再允许程序集加载。我得到这样的东西:

fact = System.Data.Common.DbProviderFactories.GetFactory("System.Data.SqlClient");

结果:

A first chance exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll

Additional information: Could not load file or assembly 'System.Data.OracleClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Either a required impersonation level was not provided, or the provided impersonation level is invalid. (Exception from HRESULT: 0x80070542)

我似乎无法做任何事情来超越这个。我在其他地方读到,这可能是因为低权限帐户无法模拟管理员帐户,至少在没有 UAC 提示的情况下是如此。

如果这是真的,IIS 是如何做到这一点的?因为我通过模拟做了很多疯狂的 ASP.Net 事情,但以前从未遇到过这种情况。

我们当然可以使用管理服务帐户运行此工具。但这样做似乎很愚蠢 - 如果冒充存在,并且它正是出于这种目的而存在,我想知道我做错了什么以及为什么它不起作用。

最佳答案

我相信我已经找到了这个问题的答案。使用antiduh的概念,我找到了这个页面:

http://support.microsoft.com/kb/821546

Some Programs That Use Impersonation May Not Work Correctly After You Install Windows 2000 SP4

After you install Windows 2000 Service Pack 4 (SP4) on your computer, some programs that use impersonation may not work correctly.

This issue may occur in situations when the user account that is used to run the program does not have the "Impersonate a client after authentication" user right.

所以这让我走上了这条路。这让我在这里 http://blogs.technet.com/b/askds/archive/2008/11/25/fun-with-the-kerberos-delegation-web-site.aspx

Note This step is done to allow the IISKerbSvc (application pool identity) the ability to impersonate the user on the web server. If you look at the computer’s user right assignments you will see Impersonate a client after authentication and the IIS_WPG group is added there by default.

噢,好的。因此,我深入研究了我的服务用户帐户的本地安全策略,发现该帐户启用此功能。

但是一旦我添加了它......它就起作用了! :)

Working Example

请注意我在此处明确添加的“wwtest”用户帐户。一旦我注销/登录,一切就位了。

所以这就是答案。为了让模拟真正发挥作用,您必须是本地管理员,或者作为已经打开该标志的内置服务帐户之一运行,或者您必须进入此处并打开这在

关于c# - 如何使用 HttpListener 处理 WindowsImpersonationContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24540677/

相关文章:

c# - 对 C# 应用程序的简单、快速脚本支持

C# SqlConnection 异常 : Keyword not Supported 'Port'

c# - 具有不同消息类型的消息队列

visual-studio - 如何在 Visual Studio 2017 中更改 Ctrl+C 以复制 Word,而不是整行

visual-studio - Visual Studio,调试时的库源

c# - 测试完成后关闭连接

Mule - 如何在 http :listener? 中使用 set-session-variable

javascript - 在 highcharts 中显示 .Net Core Web API 响应

c - 有什么办法可以排除asm函数的编译吗?

c# - 使用 httpListener 创建以 URL 作为我的 IP 地址的自定义网站