有时您需要知道答案才能提出正确的问题,因此我不确定此查询的标题是否完美。无论如何,这里去了。
我开发了一个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的访问令牌,但我该怎么做呢我似乎无法在那里找到一个直接的答案。很多建筑师风格的答案都在云端。我需要开发人员风格的答案(这样做,然后这样做):)
我相信很多新手天蓝色的开发者会发现这很有用。提前谢谢。
编辑注意:此问题与Authenticate with Dynamics 365 from an Azure Function不同,因为我在同一租户和订阅中,使用时间触发器而不是Web挂钩。我的功能应用程序唤醒,连接到CRM,进行一些计算,更新CRM并重新入睡。
答案 0 :(得分:0)
我已经设法使用Azure Key Vault保护我的凭据。对于那些正在寻找相同方法的新手...这里是步骤。
在代码顶部添加以下using语句。
使用Microsoft.Azure.KeyVault;
使用Microsoft.IdentityModel.Clients.ActiveDirectory;
如果您的功能应用程序代码位于Azure门户中,则将以下内容添加到project.json文件中。
{ “构架”: { “ net46”:{ “依赖关系”:{ “ 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 =新的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步获得的值
secret1:您从第2步获得的值
secret2:您从第2步获得的值
secret1和secret2可能会有所不同,具体取决于您创建的机密数量。
于是!希望对您有帮助,如果您有任何疑问,请在此处发布,我会尽力回答。最后,我必须归功于以下对我有帮助的资源。
PS。这是用代码发布解决方案的皮塔饼。 Stackoverflow不断阻止我提交说窗口中的代码格式不正确的信息。但是,我后来意识到,项目符号点上的“自动”项目符号格式与代码插入冲突。无论哪种方式,我都认为堆栈溢出不应阻止发布,因为这可能意味着内容提供商会放弃沮丧(我们还有其他付费工作要做!)