我正在使用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);
}