没有为模拟本地计算机用户创建凭据

时间:2018-10-12 07:04:47

标签: c# impersonation credential-manager

我正在使用Impersonator类以下的课程,

https://www.daoudisamir.com/impersonate-users-in-c/

这是凭证管理器类(使用nuget,http://nuget.org/packages/CredentialManagement/

   public static class CredentialUtil
{
public static UserPass GetCredential(string target)
{
    var cm = new Credential {Target = target};
    if (!cm.Load())
    {
        return null;
    }

    //UserPass is just a class with two string properties for user and pass
    return new UserPass(cm.Username, cm.Password);
}

public static bool SetCredentials(
     string target, string username, string password, PersistanceType persistenceType)
{
   return new Credential {Target = target, Username = username, 
           Password = password, PersistanceType =  persistenceType}.Save();
}

public static bool RemoveCredentials(string target)
{
    return new Credential { Target = target }.Delete();
}

}

现在,当执行以下代码时,不会为用户User1创建凭据。可能是什么原因?

 using (new Impersonator("User1", "machine1", "Password1"))
            {
                CredentialUtil.SetCredentials("TestTarget", "TestUser", "TestPassword", PersistanceType.LocalComputer);
            }

0 个答案:

没有答案