首页 文章

验证Azure功能应用程序以在线连接到Dynamics 365 CRM

提问于
浏览
1

有时您需要知道答案才能提出正确的问题,因此我不确定此查询的 Headers 是否完美 . 无论如何这里去了 .

我开发了一个Azure功能应用程序(基于时间触发器),可以在线连接到Dynamics 365并完成一些工作 . 都好!由于这是一个POC,我想看看有什么可能,我写了下面的代码 .

IServiceManagement<IOrganizationService> orgServiceManagement;
        orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(System.Environment.GetEnvironmentVariable("OrganizationService")));

        AuthenticationCredentials authCredentials = new AuthenticationCredentials();
        authCredentials.ClientCredentials.UserName.UserName = "[Non-interactive CRM Username here]";
        authCredentials.ClientCredentials.UserName.Password = "[Password here]";
        AuthenticationCredentials tokenCredentials;

        tokenCredentials = orgServiceManagement.Authenticate(authCredentials);

        OrganizationServiceProxy organizationProxy = new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);

我的问题......显然现在POC工作了我想找到一种方法来验证针对Azure AD的功能应用程序(而不是在代码中传递凭据)并获得一个我可以用来创建我的OrganisationServiceProxy的访问令牌,但是如何做我走了这个 . 我似乎无法在那里找到一个直接的答案 . 很多建筑师风格的答案都在 Cloud 端 . 我需要开发人员风格的答案(这样做,然后这样做):)

我相信很多新手天蓝色的开发人员会发现这很有用 . 提前致谢 .

编辑注意:这个问题与Authenticate with Dynamics 365 from an Azure Function不一样,因为我在同一个租户和订阅中,使用时间触发器而不是Web挂钩 . 我的功能应用程序唤醒,连接到CRM,进行一些计算,更新CRM并重新入睡 .

1 回答

  • 0

    我已设法使用Azure Key Vault保护我的凭据 . 那些想要做同样事情的新手......这里是步骤 .

    • 登录天蓝色门户网站并创建密钥保管库,或者如果已有密钥保管库,则转到下一步 .

    • 创建密钥保管库后,转到密钥保管库的密码部分 . 您现在将为需要保护的每个凭据创建一个秘密 . 在我的情况下,我为用户名创建了一个秘密,为密码创建了另一个秘密 . 每次创建秘密时,azure都会向您发送一个秘密标识符 . 记下这一点,因为稍后您将在azure功能配置设置中使用它 .

    • 接下来,您需要转到Azure Active Directory(Azure AD) . 您需要前往App Registrations并创建一个新的应用程序注册 . 如果您创建了功能应用程序,此时无关紧要 . 此步骤只是告知Azure AD您有一个要注册的应用程序,以便它可以向您发送应用程序ID . 在创建应用程序注册时,您需要记下发布的应用程序ID . 您将再次在功能应用程序配置设置中使用它 .

    • 仍然在Azure AD中,应用程序注册会单击“密钥”并创建新密钥 . 创建密钥后,azure将为您提供密钥值 . ( Please make note of this value as this is the only time Azure will show you this value. )您的azure功能应用程序配置设置中也需要此应用程序密钥 .

    • 返回Azure Key Vault和您创建的Key Vault . 这次单击“访问策略” . 您在此处执行的操作是允许Azure AD注册的功能应用程序连接到此密钥保管库 . 单击Add New,然后选择principal,找到您在Azure AD中注册的应用程序( Do not select your function app which will also display here, you need to select the same name that you registered with Azure AD in step 3 above )然后在secret secret下,选择'Get'并单击Save .

    • 完成设置 . 其余的是代码更改,以使所有这些工作 .

    • 将以下using语句添加到代码顶部 .

    使用Microsoft.Azure.KeyVault;
    使用Microsoft.IdentityModel.Clients.ActiveDirectory;

    • 如果您的功能应用程序代码位于Azure门户中,请将以下内容添加到project.json文件中 .

    {“frameworks”:{“net46”:{“dependencies”:{“Microsoft.IdentityModel.Clients.ActiveDirectory”:“3.13.4”,“Microsoft.Azure.KeyVault”:“2.0.1-preview”,“Microsoft .AspNet.WebApi.Client“:”5.2.3“,”Microsoft.CrmSdk.CoreAssemblies“:”9.0.0.7“}}}}

    • 如果您使用的是Visual Studio,则需要确保将上述引用添加到项目中 .

    • 请查看我上面的原始帖子,看看我在代码中如何使用凭据来改变我们现在在下面的代码中的更改 .

    AuthenticationCredentials authCredentials = new AuthenticationCredentials(); authCredentials.ClientCredentials.UserName.UserName = GetKVSecret(“Secret1”,log); authCredentials.ClientCredentials.UserName.Password = GetKVSecret(“Secret2”,log);

    • 现在这里是GetKVSecret函数的代码 .
    private static string GetKVSecret(string secretName, TraceWriter log)
    {
    var adClientId = System.Environment.GetEnvironmentVariable("AppADClientID");
    var adKey = System.Environment.GetEnvironmentVariable("AppADKey");
    var secret = System.Environment.GetEnvironmentVariable(secretName);
    
    var keyVault = new KeyVaultClient(async (string authority, string resource, string scope) => {
    var authContext = new AuthenticationContext(authority);
    var credential = new ClientCredential(adClientId, adKey);
    var token = await authContext.AcquireTokenAsync(resource, credential); 
    return token.AccessToken;
    });
    string returnValue;
    try
    {
        returnValue = keyVault.GetSecretAsync(secret).Result.Value;
        log.Info("Secret retrieved from Key Vault");
    }
    catch (Exception error)
    {
        log.Error("Unable to get secrets from Azure Key Vault.", error);
        throw;
    }
    return returnValue;
    

    }

    • 最后一步,您可以看到我从配置中获取AppADClientID和AppADKey . 因此,您需要在应用设置屏幕中创建以下条目 .
      AppADClientID:您从第3步获得的值
      AppADKey:您从第4步获得的 Value
      secret1:你从第2步获得的 Value secret2:你从第2步获得的 Value
      secret1和2可能会因您创建的机密数量而异 .

    那有!我希望你觉得这很有用,如果你有任何疑问请在这里发帖,我会尽力回答 . 最后,我必须赞扬以下资源,这些资源帮助了我 .

    Link 1 Link 2

    PS . 这是一个用代码发布解决方案的皮塔饼 . Stackoverflow一直阻止我提交说我在窗口中没有正确格式化的代码 . 但是,我后来意识到这是与代码插入冲突的子弹点上的“自动”子弹格式 . 无论如何,我认为堆栈溢出不应该阻止帖子,因为它可能意味着内容提供商会沮丧地放弃(我们还有其他付费工作要做!)

相关问题