在 WPF 中模拟域用户 - UnauthorizedAccess
我试图在 WPF 应用程序中模拟域用户帐户,以便该应用程序可以写入网络上的文件夹。 域用户有足够的权限写入此位置。 我正在使用 net 上找到的一些代码来执行冒充。
基本上,它调用返回安全令牌的本机 LogonUser 方法,然后创建一个传递令牌的新 WindowsIdentity,最后调用 windowsIdentity.Impersonate() 方法。 执行上述逻辑没有出现任何异常。
调用 WindowsIdentity.GetCurrent() -> 返回模拟的身份。
写入 UNC 路径 -> 未经授权的访问异常。
因此,我在尝试写入文件之前检查 Thread.CurrentPrincipal 对象,该对象有一个 GenericPrincipal,而不是具有模拟 WindowsIdentity 的 WindowsPrincipal。
因此,在应用程序启动时,我将 AppDomain.CurrentAppDomain.SetPrincipalPolicy 设置为 PrimaryPolicy.Impersonate。
我重新启动我的应用程序...
在调用模拟之前,我可以在 Thread.CurrentPrincipal 上看到我自己的凭据,这些凭据是我用来登录到我的开发计算机并正在执行我的 WPF 程序的凭据。
我再次尝试运行模拟逻辑,再次在 WindowsIdentity.GetCurrent 上看到模拟身份,一切似乎都很好,没有例外
但是在 Thread.GetCurrentPrincipal 上我仍然看到自己的凭据,如果我查看 AuthenticationType 属性,有一个UnauthorizedException(仅在调试器中可见,不会在应用程序中抛出!!)。 我让代码运行。
再次尝试在 UNC 位置写入我的文件时出现 UnauthorizedAccess。
我尝试的最后一件事是使用 WindowsIdentity.GetCurrent() 创建一个新的 WindowsPrincipal 我明确地将其设置在 Thread.Current 上,但结果相同。
写入 UNC 位置时的 UnauthorizedAccess。
我没有主意了:)
I am trying to impersonate a domain user account in a WPF application so the application can write to a folder on the network. The domain user has sufficient rights to write to this location.
I'm using some code found on the net to perform the impersonation.
Basically, it calls the native LogonUser method which returns a security token, then create a new WindowsIdentity passing the token and finally calling the windowsIdentity.Impersonate() method.
I get no exceptions executing above logic.
Calling WindowsIdentity.GetCurrent() -> returns impersonated identity.
Writing to the UNC path -> UnauthorizedAccess exception.
So, I inspect the Thread.CurrentPrincipal object before I try to write the file, this has a GenericPrincipal, and not a WindowsPrincipal with the impersonated WindowsIdentity.
So in the startup of the application I set the AppDomain.CurrentAppDomain.SetPrincipalPolicy to PrincipalPolicy.Impersonate.
I restart my application...
Before my call to impersonate, i can see my own credentials on Thread.CurrentPrincipal, the ones that i'm using to log onto my development machine and which is executing my WPF program.
I again try to run the impersonation logic, again I see the impersonated identity on WindowsIdentity.GetCurrent, all seems fine, no exceptions
However on Thread.GetCurrentPrincipal I still see my own credentials, and if I look at the AuthenticationType property, there is an UnauthorizedException (which is only visible in the debugger, it is not being thrown in the application !!). I let the code run.
Again, UnauthorizedAccess when trying to write my file on the UNC location.
Last thing I tried is to create a new WindowsPrincipal with WindowsIdentity.GetCurrent()
and I explicitly set it on Thread.Current, but same result.
UnauthorizedAccess when a write to the UNC location.
I'm out of ideas :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
具有网络共享所属的机器是否在域中?
您是否尝试使用该域帐户访问网络共享? 例如使用“运行方式...”。
在 LogonUser 函数中,在登录时尝试使用 LOGON32_LOGON_NETWORK 标志。
另外,要查看模拟是否确实发生,请尝试从安全策略启用登录/注销事件审核 (信息链接)
Does the machine with network share belong is in the domain?
Did you try accessing network share using that domain account? For instance using "Run As...".
In LogonUser function try to use LOGON32_LOGON_NETWORK flag, when doing logon.
Also to see if impersonation actually took place try to enable auditing of Logon/Logoff events from security policy (info link)