理解.net冒充
.NET模擬允許在指定的用戶帳戶下執行代碼,並經常與基於憑證的帳戶訪問相結合。 .NET框架為模擬和用戶帳戶管理提供了必要的API。
模擬技術
名稱空間提供了幾種實現模仿的方法:System.Security.Principal
WindowsIdentity.RunImpersonated
或委託。
Action
Func<T>
WindowsIdentity.Impersonate
塊中的模仿。 WindowsImpersonationContext
using
>帶有憑據的用戶帳戶>使用提供的憑據訪問用戶帳戶通常涉及本機Win32 api:
雖然功能,但LogonUser
可以復雜。 一個更簡單的替代方法是
<code class="language-csharp">[DllImport("advapi32.dll")] internal static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);</code>
LogonUser
SimpleImpersonation
限制:遠程冒充
<code class="language-csharp">using SimpleImpersonation; var credentials = new UserCredentials(domain, username, password); using (SafeAccessTokenHandle userHandle = credentials.LogonUser(LogonType.Interactive)) { // Your impersonated code here }</code>
模仿本質上是本地的。除非它們共享相同的域或具有信任關係,否則它不會擴展到遠程計算機。
以上是如何在.NET中執行模仿?的詳細內容。更多資訊請關注PHP中文網其他相關文章!