Exemplo n.º 1
0
        private Task <string> GetAcrRefreshTokenAsync()
        {
            return(_acrRefreshToken.GetValueAsync(async() =>
            {
                StringContent oauthExchangeBody = new StringContent(
                    $"grant_type=access_token&service={_acrName}&tenant={_tenant}&access_token={_aadAccessToken}",
                    Encoding.UTF8,
                    "application/x-www-form-urlencoded");

                HttpResponseMessage tokenExchangeResponse = await _httpClient.PostAsync(
                    $"https://{_acrName}/oauth2/exchange", oauthExchangeBody);
                tokenExchangeResponse.EnsureSuccessStatusCode();
                OAuthExchangeResult acrRefreshTokenResult = JsonConvert.DeserializeObject <OAuthExchangeResult>(
                    await tokenExchangeResponse.Content.ReadAsStringAsync());
                return acrRefreshTokenResult.RefreshToken;
            }));
        }
Exemplo n.º 2
0
        protected override async Task <string> GetBearerTokenAsync(IEnumerable <string> repos)
        {
            AuthenticationContext authContext = new AuthenticationContext($"https://login.microsoftonline.com/{this.Registry.Tenant}");
            AuthenticationResult  result      = await authContext.AcquireTokenAsync(
                "https://management.azure.com", new ClientCredential(this.Registry.ClientId, this.Registry.ClientSecret));

            string aadAccessToken = result.AccessToken;

            FormUrlEncodedContent oauthExchangeBody = new FormUrlEncodedContent(new Dictionary <string, string>
            {
                { "grant_type", "access_token" },
                { "service", this.Registry.HostName },
                { "tenant", this.Registry.Tenant },
                { "access_token", aadAccessToken }
            });

            HttpResponseMessage tokenExchangeResponse = await this.HttpClient.PostAsync(
                $"https://{this.Registry.HostName}/oauth2/exchange", oauthExchangeBody);

            tokenExchangeResponse.EnsureSuccessStatusCode();
            OAuthExchangeResult acrRefreshTokenResult = JsonConvert.DeserializeObject <OAuthExchangeResult>(
                await tokenExchangeResponse.Content.ReadAsStringAsync());

            var fields = new Dictionary <string, string>
            {
                { "grant_type", "refresh_token" },
                { "service", this.Registry.HostName },
                { "refresh_token", acrRefreshTokenResult.RefreshToken }
            };

            foreach (string repo in repos)
            {
                fields.Add("scope", $"repository:{repo}:pull");
            }

            FormUrlEncodedContent oauthTokenBody = new FormUrlEncodedContent(fields);

            HttpResponseMessage tokenResponse = await HttpClient.PostAsync(
                $"https://{this.Registry.HostName}/oauth2/token", oauthTokenBody);

            tokenResponse.EnsureSuccessStatusCode();
            OAuthTokenResult acrAccessTokenResult = JsonConvert.DeserializeObject <OAuthTokenResult>(
                await tokenResponse.Content.ReadAsStringAsync());

            return(acrAccessTokenResult.AccessToken);
        }