【发布时间】:2016-01-12 17:54:11
【问题描述】:
所以我试图在 AD 中更改用户的密码。我有一个运行服务器 2008 且具有 AD 角色的 VM(这仅用于测试目的。当此 Web 应用上线时将连接到本地 AD 服务器),以及一个在同一个 Vnet 中具有 Web 作业的 Azure Web 应用。
当我从我的机器上的 Visual Studio 运行网络作业时,它按预期工作。但是当我在 Azure 中运行 webjob 时,它会得到一个 Access is Denied 异常。
因此,不知何故,在 Azure 中运行时,并没有使用赋予该方法的标识。它可能使用的是应用程序池中的默认标识,但由于它是一个 WebApp,我无法更改应用程序池中的标识,因为 WebApps 在沙盒环境中运行。
我也尝试过使用模拟,但也不起作用,访问仍然被拒绝。
有什么想法吗?
谢谢
代码:
//The credentials of an admin service account in AD which are retrieved from a config file
private static string strUsername;
private static string strPassword;
//The domain name of the AD server, also from config
private static string strDomain;
static void Main(string[] args)
{
try
{
string mess;
//Password here just for testing purposes
SetUserPassword("pname@domain.com", "newP45sword", out mess);
}
catch (Exception ex)
{
Console.WriteLine("Error..." + ex);
}
}
public static void SetUserPassword(string sUserName, string sNewPassword, out string sMessage)
{
try
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
oUserPrincipal.SetPassword(sNewPassword);//This is where the exception occurs
sMessage = "";
}
catch (Exception ex)
{
Console.WriteLine("Err." + ex);
}
}
public static PrincipalContext GetPrincipalContext()
{
//Here is where the admin credentials are passed to the app
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, strDomain, strUsername, strPassword);
return oPrincipalContext;
}
public static UserPrincipal GetUser(string sUserName)
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
return oUserPrincipal;
}
【问题讨论】:
标签: c# azure active-directory azure-webjobs change-password