asp.net - 在 ASP.Core 中找不到 WindowsImpersonationContext 和 Impersonate()

标签 asp.net .net asp.net-core .net-core impersonation

我在 .NET Framework 4.0 程序集中有以下内容:

var newId= new WindowsIdentity(duplicateTokenHandle);
WindowsImpersonationContext newId = ImpersonatedIdentity.Impersonate();

我正在将它移植到 ASP.Core,但是 WindowsImpersonationContextWindowsIdentity.Impersonate()没有找到。

我试过添加以下内容:
  • System.Security.Claims 4.3.0
  • System.Security.Principal 4.3.0
  • System.Security.Principal.Windows 4.4.0

  • 如何在 ASP.Core 中执行模拟?

    更新

    .NET Core 或 .NET Standard 似乎不支持它 - 是否有解决方法,或者我是否必须辞职以针对框架?

    最佳答案

    死灵法术。
    像这样:

    using System.Security.Principal;
    
    
    namespace regeditor
    {
    
    
        public class WindowsLogin : System.IDisposable
        {
            protected const int LOGON32_PROVIDER_DEFAULT = 0;
            protected const int LOGON32_LOGON_INTERACTIVE = 2;
    
            public WindowsIdentity Identity = null;
            private System.IntPtr m_accessToken;
    
    
            [System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError = true)]
            private static extern bool LogonUser(string lpszUsername, string lpszDomain,
            string lpszPassword, int dwLogonType, int dwLogonProvider, ref System.IntPtr phToken);
    
            [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
            private extern static bool CloseHandle(System.IntPtr handle);
    
    
            // AccessToken ==> this.Identity.AccessToken
            //public Microsoft.Win32.SafeHandles.SafeAccessTokenHandle AT
            //{
            //    get
            //    {
            //        var at = new Microsoft.Win32.SafeHandles.SafeAccessTokenHandle(this.m_accessToken);
            //        return at;
            //    }
            //}
    
    
            public WindowsLogin()
            {
                this.Identity = WindowsIdentity.GetCurrent();
            }
    
    
            public WindowsLogin(string username, string domain, string password)
            {
                Login(username, domain, password);
            }
    
    
            public void Login(string username, string domain, string password)
            {
                if (this.Identity != null)
                {
                    this.Identity.Dispose();
                    this.Identity = null;
                }
    
    
                try
                {
                    this.m_accessToken = new System.IntPtr(0);
                    Logout();
    
                    this.m_accessToken = System.IntPtr.Zero;
                    bool logonSuccessfull = LogonUser(
                       username,
                       domain,
                       password,
                       LOGON32_LOGON_INTERACTIVE,
                       LOGON32_PROVIDER_DEFAULT,
                       ref this.m_accessToken);
    
                    if (!logonSuccessfull)
                    {
                        int error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                        throw new System.ComponentModel.Win32Exception(error);
                    }
                    Identity = new WindowsIdentity(this.m_accessToken);
                }
                catch
                {
                    throw;
                }
    
            } // End Sub Login 
    
    
            public void Logout()
            {
                if (this.m_accessToken != System.IntPtr.Zero)
                    CloseHandle(m_accessToken);
    
                this.m_accessToken = System.IntPtr.Zero;
    
                if (this.Identity != null)
                {
                    this.Identity.Dispose();
                    this.Identity = null;
                }
    
            } // End Sub Logout 
    
    
            void System.IDisposable.Dispose()
            {
                Logout();
            } // End Sub Dispose 
    
    
        } // End Class WindowsLogin 
    
    
    } // End Namespace 
    

    用法:
    *// WindowsIdentity user = (WindowsIdentity)context.User.Identity;
    // using (WindowsIdentity user = WindowsIdentity.GetCurrent())
    
    using (WindowsLogin wi = new WindowsLogin("Administrator", System.Environment.MachineName, "TOP_SECRET"))
    {
        #if NET461
        using (user.Impersonate())
        #else
        WindowsIdentity.RunImpersonated(wi.Identity.AccessToken, () =>
        #endif
        {
            WindowsIdentity useri = WindowsIdentity.GetCurrent();
            System.Console.WriteLine(useri.Name);
        }
        #if !NET461
        );
        #endif
    
    }*
    

    关于asp.net - 在 ASP.Core 中找不到 WindowsImpersonationContext 和 Impersonate(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46529121/

    相关文章:

    c# - Asp.net core + EF Code优先,迁移不同项目中的文件

    asp.net-mvc - 添加 Razor View 时 VS2017 ASP .NET Core 错误

    c# - 如何在没有控制问题的情况下停用 ViewState

    c# - asp.net 缓存限制?

    c# - Entity Framework - 外键组件......不是类型的声明属性

    c# - .NET 和 C# 的初学者书籍?

    asp.net - 如何从 SQL 查询中过滤具有唯一 ID 的旧条目

    jquery - 无法在浏览器中手动访问 ASMX Web 服务,但可以使用 jQuery

    c# - 在具有 32 个以上逻辑内核的系统上使用 Process.ProcessorAffinity

    asp.net - ASP.NET Web应用程序(.NET Framework)与ASP.NET Core Web应用程序(.NET Framework)