我们有一个特定的 SQL Server,我们需要从厚客户端(.Net 4.0 WPF)访问它,并且我们用于该连接的唯一凭据是“服务”帐户,该帐户实际上是具有 SQL 权限的 Active Directory 帐户服务器。
我在整个项目中使用实体框架和 ObjectContext,所以我在这里继续使用它。环顾四周后,我实现了一个基于 LogonUserEx 和 DuplicateTokenEx 的模拟例程,它允许我通过依赖注入编写以下内容:
using (container.Resolve<Impersonate>())
using (var context = container.Resolve<MyObjectContext>())
{
context.Connection.Open();
//Do some work with the data as the service account.
context.Connection.Close();
}
上面Impersonate类的构造函数调用了LogonUserEx等等。我明确打开和关闭连接作为工作单元模式的一部分,这不应该是相关的。
现在,通过调试我发现服务帐户的令牌已成功检索,并且用户被“模拟”。但是,当我尝试实例化 ObjectContext 时,我收到以下错误:
System.TypeInitializationException: The type initializer for 'EntityBid' threw a
n exception. ---> System.IO.FileLoadException: Could not load file or assembly '
System.Data.Entity.dll' or one of its dependencies. Either a required impersonat
ion level was not provided, or the provided impersonation level is invalid. (Exc
eption from HRESULT: 0x80070542)
at System.Runtime.InteropServices.Marshal.GetHINSTANCE(RuntimeModule m)
at System.Runtime.InteropServices.Marshal.GetHINSTANCE(Module m)
at EntityBid.initEntryPoint()
at EntityBid.internalInitialize()
at EntityBid..cctor()
--- End of inner exception stack trace ---
at EntityBid.Trace(String fmtPrintfW, String a1)
at System.Data.EntityUtil.ProviderExceptionWithMessage(String message, Except
ion inner)
at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean op
enCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection
, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnection
OnFailure)
at System.Data.EntityClient.EntityConnection.Open()
at Core.Security.TestHarness._2.Class1..ctor()
因此,我所模拟的帐户似乎不再具有从 GAC 加载 DLL 的访问权限或足够的权限。 Fusion 日志没有提供任何附加信息。
我不知道如何解决这个问题。我想知道我当前是否没有检索到具有足够权限的令牌。请注意,我向 LogonUserEx 提供以下参数:LOGON32_LOGON_NETWORK_CLEARTEXT 和 LOGON32_PROVIDER_DEFAULT。
最后,请注意,此过程在具有登录用户管理权限的计算机上绝对可以正常工作。当我在具有“普通”帐户的用户上运行它时,它会崩溃,并受通常的公司 GPO 的约束!
编辑:只是为了包含模拟的“重要”部分。请注意,我现在已切换到 LOGON32_LOGON_UNLOCK,因为它工作得更好。对于格式稍有问题的情况深表歉意:
if (LogonUserEx(dUser, dDomain, dPassword, LOGON32_LOGON_UNLOCK,
LOGON32_PROVIDER_DEFAULT, out token, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero))
{
if (DuplicateTokenEx(token, MAXIMUM_ALLOWED, ref sa, SECURITY_IMPERSONATION_LEVEL.SecurityDelegation, TOKEN_TYPE.TokenPrimary, out tokenDuplicate))
{
m_ImpersonatedUser = new WindowsIdentity(token);
_windowsImpersonationContext = m_ImpersonatedUser.Impersonate();
非常感谢任何帮助。
Nick.