Beispiel #1
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var    token        = request.DataStore.GetJson("AzureToken");
            string refreshToken = request.DataStore.GetJson("AzureToken", "refresh_token");
            string aadTenant    = request.DataStore.GetValue("AADTenant");

            string tokenUrl        = string.Format(Constants.AzureTokenUri, aadTenant);
            var    primaryResponse = await GetToken(refreshToken, tokenUrl, Constants.MicrosoftClientId);

            primaryResponse.Add("id_token", token["id_token"]);

            JObject crmToken      = new JObject();
            JObject keyvaultToken = new JObject();

            if (request.DataStore.GetValue("MsCrmToken") != null)
            {
                crmToken = GetAzureToken.RetrieveCrmToken(primaryResponse["refresh_token"].ToString(), request.Info.WebsiteRootUrl, request.DataStore);
            }

            if (request.DataStore.GetValue("AzureTokenKV") != null)
            {
                keyvaultToken = await GetToken(refreshToken, tokenUrl, Constants.MicrosoftClientIdCrm);

                keyvaultToken.Add("id_token", token["id_token"]);
            }

            var obj = new JObject(new JProperty("AzureToken", primaryResponse),
                                  new JProperty("MsCrmToken", crmToken),
                                  new JProperty("AzureTokenKV", keyvaultToken));

            return(new ActionResponse(ActionStatus.Success, obj, true));
        }
Beispiel #2
0
        public static JObject GetTokenForResource(ActionRequest request, JToken azureToken, string resource)
        {
            JObject tokenObj;

            using (HttpClient httpClient = new HttpClient())
            {
                string tenantId     = AzureUtility.GetTenantFromToken(azureToken);
                string refreshToken = AzureUtility.GetRefreshToken(azureToken);
                string tokenUrl     = string.Format(Constants.AzureTokenUri, tenantId);

                string        clientId = GetAzureToken.GetClientIdFromRequest(request);
                string        token    = GetAzureToken.GetTokenUri2(refreshToken, resource, request.Info.WebsiteRootUrl, clientId);
                StringContent content  = new StringContent(token);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
                string response2 = httpClient.PostAsync(new Uri(tokenUrl), content).Result.Content.AsString();
                tokenObj = JsonUtility.GetJsonObjectFromJsonString(response2);
            }

            return(tokenObj);
        }