【问题标题】:UserPrincipal.SetPassword Access Denied in Azure WebJobAzure WebJob 中的 UserPrincipal.SetPassword 访问被拒绝
【发布时间】: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


    【解决方案1】:

    是的,这很可能是 Azure WebApp 沙盒问题。有关沙盒限制的详细信息,请参阅in the wiki here。该 wiki 没有显式调用您尝试使用的 API,但正如 another SO post 中针对同一 UserPrincipal.SetPassword API 所指出的那样,该 API 会导致调用 [SecurityCritical] 方法的调用堆栈。

    恐怕你很可能不得不找到另一种方法来完成你的场景。

    【讨论】:

    • 这很不幸,但正是我所期待的。如果我改为使用具有辅助角色的云服务,是否可以解决此问题?
    • 不确定。可能是因为您拥有虚拟机,所以不会有沙盒限制。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2017-05-17
    • 2015-11-23
    相关资源
    最近更新 更多