在其他用户上安装PFX证书

朱利安

下面的代码可以完美地在当前用户上安装pfx。我想通过提供用户名和密码(而不必使用该用户登录)将其安装在另一个用户上。我前一阵子用批处理文件做到了,我怎么能用C#做到这一点呢?

我已经尝试了一些方法,包括模拟,但是无法使其正常工作。

X509Certificate2 certificate = new X509Certificate2("C:\\teste\\cert.pfx", "password");
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Add(certificate);
store.Close();

更新:

多亏了Bill的代码,只要用户登录,该过程就可以正常工作。注销后,尝试安装pfx时会引发异常。“系统找不到指定的文件”。如果用户重新登录,它将再次起作用!!

该代码是非常有用的,但是如果用户脱机时它也可以工作,则非常适合该工作!有办法吗?

提前致谢!

// obtains user token
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,
    int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

// closes open handes returned by LogonUser
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);

public void DoWorkUnderImpersonation() {
    //elevate privileges before doing file copy to handle domain security
    WindowsImpersonationContext impersonationContext = null;
    IntPtr userHandle = IntPtr.Zero;
    const int LOGON32_PROVIDER_DEFAULT = 0;
    const int LOGON32_LOGON_INTERACTIVE = 2;
    string domain = ConfigurationManager.AppSettings["ImpersonationDomain"];
    string user = ConfigurationManager.AppSettings["ImpersonationUser"];
    string password = ConfigurationManager.AppSettings["ImpersonationPassword"];

    try {
        Console.WriteLine("windows identify before impersonation: " + WindowsIdentity.GetCurrent().Name);

        // if domain name was blank, assume local machine
        if (domain == "")
            domain = System.Environment.MachineName;

        // Call LogonUser to get a token for the user
        bool loggedOn = LogonUser(user,
                                    domain,
                                    password,
                                    LOGON32_LOGON_INTERACTIVE,
                                    LOGON32_PROVIDER_DEFAULT,
                                    ref userHandle);

        if (!loggedOn) {
            Console.WriteLine("Exception impersonating user, error code: " + Marshal.GetLastWin32Error());
            return;
        }

        // Begin impersonating the user
        impersonationContext = WindowsIdentity.Impersonate(userHandle);

        Console.WriteLine("Main() windows identify after impersonation: " + WindowsIdentity.GetCurrent().Name);

        //run the program with elevated privileges (like file copying from a domain server)
        DoWork();

    } catch (Exception ex) {
        Console.WriteLine("Exception impersonating user: " + ex.Message);
    } finally {
        // Clean up
        if (impersonationContext != null) {
            impersonationContext.Undo();
        }

        if (userHandle != IntPtr.Zero) {
            CloseHandle(userHandle);
        }
    }
}


private void DoWork() {
    //everything in here has elevated privileges
    X509Certificate2 certificate = new X509Certificate2("C:\\teste\\cert.pfx", "password");
    X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    store.Open(OpenFlags.ReadWrite);
    store.Add(certificate);
    store.Close();
}
比尔·桑布朗

您是如何进行假冒的?以前,我已经成功使用过此答案中的模拟代码段:如何正确使用LogonUser从工作组客户端模拟域用户

我使用它的方式是将其包装在DLL中,然后从powershell调用它。它可以用于访问该用户的证书存储,从而使StoreLocation.CurrentUser能够执行其操作。

要将其应用于您的情况,您可以尝试:

// obtains user token
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,
    int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

// closes open handes returned by LogonUser
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);

public void DoWorkUnderImpersonation() {
    //elevate privileges before doing file copy to handle domain security
    WindowsImpersonationContext impersonationContext = null;
    IntPtr userHandle = IntPtr.Zero;
    const int LOGON32_PROVIDER_DEFAULT = 0;
    const int LOGON32_LOGON_INTERACTIVE = 2;
    string domain = ConfigurationManager.AppSettings["ImpersonationDomain"];
    string user = ConfigurationManager.AppSettings["ImpersonationUser"];
    string password = ConfigurationManager.AppSettings["ImpersonationPassword"];

    try {
        Console.WriteLine("windows identify before impersonation: " + WindowsIdentity.GetCurrent().Name);

        // if domain name was blank, assume local machine
        if (domain == "")
            domain = System.Environment.MachineName;

        // Call LogonUser to get a token for the user
        bool loggedOn = LogonUser(user,
                                    domain,
                                    password,
                                    LOGON32_LOGON_INTERACTIVE,
                                    LOGON32_PROVIDER_DEFAULT,
                                    ref userHandle);

        if (!loggedOn) {
            Console.WriteLine("Exception impersonating user, error code: " + Marshal.GetLastWin32Error());
            return;
        }

        // Begin impersonating the user
        impersonationContext = WindowsIdentity.Impersonate(userHandle);

        Console.WriteLine("Main() windows identify after impersonation: " + WindowsIdentity.GetCurrent().Name);

        //run the program with elevated privileges (like file copying from a domain server)
        DoWork();

    } catch (Exception ex) {
        Console.WriteLine("Exception impersonating user: " + ex.Message);
    } finally {
        // Clean up
        if (impersonationContext != null) {
            impersonationContext.Undo();
        }

        if (userHandle != IntPtr.Zero) {
            CloseHandle(userHandle);
        }
    }
}


private void DoWork() {
    //everything in here has elevated privileges
    X509Certificate2 certificate = new X509Certificate2("C:\\teste\\cert.pfx", "password");
    X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    store.Open(OpenFlags.ReadWrite);
    store.Add(certificate);
    store.Close();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

全局npm软件包安装在其他用户目录上

rustc 安装在其他用户的主目录时如何执行?

使用其他用户证书的Java Rest调用

通过root cron在其他用户上启动GUI应用程序显示

TortoiseGit-在其他用户上推送错误

在Windows中为其他用户安装软件

无法以其他用户身份安装Elasticsearch

该帐户无权模拟所请求的用户,同时在其他用户的Outlook日历上保存约会

隐藏FTP上其他用户的目录

无法登录Debian系统上的其他用户

检查应用程序是否未在其他用户帐户上运行 c#

NodeJS:在其他用户更改权限后,强制用户刷新会话

ConvertTo-SecureString在其他用户帐户上没有密钥运行,是否有办法(具有正确的凭据)从其他用户获得此功能?

其他用户收到消息

其他用户的国家数

从其他用户销毁会话

SpringSecurityService:注销其他用户?

Magento 2自定义模块无法在其他用户中访问

是否可以在其他用户下运行goroutine或go方法?

如何在其他用户使用Oledb的同时阅读excel?

在其他用户主目录中使用root到mkdir

samba-在其他用户主目录中共享时,权限被拒绝

尝试在其他用户主目录中查找空文件

通过ssh在其计算机上以其他用户身份运行命令?

在其他用户创建的文件夹中创建文件

如何为root用户以外的其他用户安装驱动器?

将超级用户中安装的软件访问给其他用户

为什么我的私人用户到用户的聊天显示在其他用户的聊天资料中?

我如何在没有提醒/通知的情况下在其他用户的Google日历上创建活动