Friday, 22 December 2023

Azure Key Vault Connection to connect to MS CRM

 Below is the sample code regarding how to connect to Azure Key Vault to get Credentials to connect to any Ms CRM Organizations 


 // Azure Key Vault Connections 

            string keyVaultName = ConfigurationManager.AppSettings["KeyVaultName"];

            string crmUrl = ConfigurationManager.AppSettings["crmUrl"];


            string password = "";

            var kvUri = String.Format(ConfigurationManager.AppSettings["KeyVaultUrl"], keyVaultName);


            var credential = new Azure.Identity.ClientSecretCredential(ConfigurationManager.AppSettings["KeyVaultAzureTenantID"], ConfigurationManager.AppSettings["KeyVaultAzureClientID"], ConfigurationManager.AppSettings["KeyVaultAzureClientSecret"]);

            var client = new SecretClient(new Uri(kvUri), credential);


            var user = client.GetSecret(ConfigurationManager.AppSettings["KeyValueUserIDSecret"]).Value;

            userName = user.Value;


            var pwd = client.GetSecret(ConfigurationManager.AppSettings["KeyValuePasswordSecret"]).Value;

            password = pwd.Value;


            string connectionString = string.Format("AuthType=OAuth;Username={0};Password={1};Url={2};AppId=760c6b53-9d70-4fe0-8b08-2b6001f7069d;RedirectUri=app://468550f1-718b-416c-bd12-a686589aadbb;LoginPrompt=Never;RequireNewInstance = True", userName, password, crmUrl);


            var conn = new CrmServiceClient(connectionString);


IOrganizationService service;

            service = (IOrganizationService)conn.OrganizationWebProxyClient ?? (IOrganizationService)conn.OrganizationServiceProxy;


No comments:

Post a Comment