您可以使用模拟来以不同的用户身份运行代码。在CodeProject上有一个很好的模拟类,http://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User。
只需在机器上创建一个新的管理员帐户,即可进行自己想要的操作,将凭证存储在应用程序设置的web.config中(如果您想要使用rijandael并使用代码进行解密,请将其加密)
但从长期储存总之,你使用它像这样,
using (new Impersonator("myUsername", "myDomainname", "myPassword"))
{
//This code is running elevated as the specified user
}
//This code is reverted back to the previous user.
其实,我写我自己的web.config的ConfigurationElement存储凭据。它看起来像这样,
public class CredentialConfigurationElement : ConfigurationElement
{
#region Properties
[ConfigurationProperty("userName", DefaultValue="", IsRequired=true)]
public string UserName
{
get { return (string)this["userName"];}
set { this["userName"] = value; }
}
[ConfigurationProperty("password", DefaultValue = "", IsRequired = true)]
public string Password
{
get { return (string)this["password"]; }
set { this["password"] = value; }
}
#endregion
#region Explicit Operators
public static implicit operator NetworkCredential(CredentialConfigurationElement value)
{
return new NetworkCredential(value.UserName, value.Password);
}
public static implicit operator CredentialConfigurationElement(NetworkCredential value)
{
return new CredentialConfigurationElement() { UserName = value.UserName, Password = value.Password };
}
#endregion
}
但是,要使用它,您需要创建一个Custom ConfigurationSection,这是一个从ConfigurationSection继承的类,并将CredentialConfigurationElement作为属性公开。
E.g.您可以创建一个名为CodeImpersonatorSection的新节:ConfigurationSection
并在此处将CredentialConfigurationElement公开为名为ImpersonationCredentials的属性。
然后使用(CodeImpersonatorSection)WebConfigurationManager.GetSection(“/ yoursectionnamehere”);获取配置实例。
有选择地修改冒领类自动做到这一点,并改变它有一个静态方法类似
Impersonator.RunUnderImpersonation(p => {
//This code is impersonating the configured user.
});
免费被高估了。如果您购买价值1000美元的解决方案,但可以节省数天的研究,编码,调试和支持工作,这非常值得花费。 – 2014-09-02 14:13:17
不幸的是我不在财务决策部门,所以自由是唯一不幸的方法。 – anothershrubery 2014-09-02 14:32:38
或者你可以向决策者解释这一点。 – 2014-09-02 15:01:02