简体   繁体   English

Azure WebJob中的UserPrincipal.SetPassword访问被拒绝

[英]UserPrincipal.SetPassword Access Denied in Azure WebJob

So I am attempting to change the password of a user within AD. 因此,我试图在AD中更改用户的密码。 I have a VM running server 2008 with an AD role (Which is just for testing purposes. When live this webapp will connect to an on-premise AD server), and an Azure WebApp with a webjob within the same Vnet. 我有一个运行具有AD角色的Server 2008的VM(仅用于测试目的。此Web应用程序上线时将连接到本地AD服务器),以及一个具有Webjob的Azure WebApp在同一Vnet中。

When I run the webjob from visual studio on my machine, it works as intended. 当我从计算机上的Visual Studio运行webjob时,它可以正常工作。 But when I run the webjob in Azure, it gets an Access is Denied exception. 但是,当我在Azure中运行webjob时,它得到了Access is Denied异常。

So somehow, the identity given to the method isn't being used when it's run in Azure. 因此,以某种方式在Azure中运行时不会使用赋予该方法的标识。 It is probably using the default identity from the App Pool, but due to it being a WebApp, I am unable to change the identity in the App Pool, because WebApps run in a sandboxed environment. 它可能正在使用应用程序池中的默认标识,但是由于它是WebApp,因此我无法更改应用程序池中的标识,因为WebApps在沙盒环境中运行。

I have also tried using impersonation, which doesn't work either, access is still denied. 我还尝试过使用模拟功能,该方法也不起作用,访问仍然被拒绝。

Any ideas? 有任何想法吗?

Thanks 谢谢

Code: 码:

//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;
}

Yes this is most likely an Azure WebApp Sandbox issue. 是的,这很可能是Azure WebApp沙箱问题。 Details on sandbox restrictions can be found in the wiki here . 有关沙箱限制的详细信息,请参见此处的Wiki That wiki doesn't explicitly call out the API you're attempting to use, but as indicated in another SO post for the same UserPrincipal.SetPassword API, that API results in a call stack that invokes a [SecurityCritical] method. 该Wiki不会显式调用您尝试使用的API,但是正如同一UserPrincipal.SetPassword API的另一篇SO文章所述,该API会导致调用[SecurityCritical]方法的调用堆栈。

You're most likely going to have to find another way to accomplish your scenario I'm afraid. 恐怕您很可能必须找到另一种方式来完成您的方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM