Exemplo n.º 1
0
        public static async Task <DnfClient> LoginAsync(Uri loginUri, string clientId, string clientSecret, string userName, string password, Action <string>?logger = null)
        {
            if (loginUri == null)
            {
                throw new ArgumentNullException(nameof(loginUri));
            }
            if (clientId == null)
            {
                throw new ArgumentNullException(nameof(clientId));
            }
            if (clientSecret == null)
            {
                throw new ArgumentNullException(nameof(clientSecret));
            }
            if (userName == null)
            {
                throw new ArgumentNullException(nameof(userName));
            }
            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }
            logger?.Invoke("DnfClient connecting...");
            var timer = Stopwatch.StartNew();

            using var auth = new AuthenticationClient { ApiVersion = DefaultApiVersion };
            var tokenRequestEndpointUrl = new Uri(new Uri(loginUri.GetLeftPart(UriPartial.Authority)), "/services/oauth2/token").ToString();
            await auth.UsernamePasswordAsync(clientId, clientSecret, userName, password, tokenRequestEndpointUrl)
            .ConfigureAwait(false);

            logger?.Invoke($"DnfClient connected ({timer.Elapsed.TotalSeconds} seconds)");

            var client = new DnfClient(auth.InstanceUrl, auth.AccessToken, auth.RefreshToken, logger)
            {
                Id = auth.Id
            };

            return(client);
        }
Exemplo n.º 2
0
        public static async Task <DnfClient> OAuthLoginAsync(OAuthProfile oAuthProfile, Action <string>?logger = null)
        {
            if (oAuthProfile.LoginUri == null)
            {
                throw new ArgumentNullException(nameof(oAuthProfile.LoginUri));
            }
            logger?.Invoke("DnfClient connecting...");
            var timer = Stopwatch.StartNew();

            using var auth = new AuthenticationClient { ApiVersion = DefaultApiVersion };
            var tokenRequestEndpointUrl = new Uri(new Uri(oAuthProfile.LoginUri.GetLeftPart(UriPartial.Authority)), "/services/oauth2/token").ToString();
            await auth.WebServerAsync(oAuthProfile.ClientId, oAuthProfile.ClientSecret, oAuthProfile.RedirectUri, oAuthProfile.Code, tokenRequestEndpointUrl)
            .ConfigureAwait(false);

            logger?.Invoke($"DnfClient connected ({timer.Elapsed.TotalSeconds} seconds)");

            var client = new DnfClient(auth.InstanceUrl, auth.AccessToken, auth.RefreshToken, logger)
            {
                Id = auth.Id
            };

            return(client);
        }