示例#1
0
        protected override async Task OnBeginLoginAsync()
        {
            if (CanRefresh)
            {
                Logger.WriteInfo($"Discovered refresh token...reconnecting to service at url {ServiceUri} using refresh token...");
                await OnRefreshAccessTokenAsync();
            }
            else
            {
                Logger.WriteInfo($"Connecting to service at url {ServiceUri} as user {Username} (Client Id {ClientId})");
                var uri     = new Uri(ServiceUri, "oauth/token");
                var request = new FormServiceRequest <OAuthTokenResult>(uri, HttpVerb.Post);
                var data    = new OAuthAccessTokenRequestData()
                {
                    ClientId = ClientId, ClientSecret = ClientSecret, Email = _username, Password = _password, GrantType = GrantTypes.Password
                };
                var token = await request.ExecuteAsync(data.AsDictionary());

                if (token != null)
                {
                    if (!string.IsNullOrWhiteSpace(token.AccessToken) && !string.IsNullOrWhiteSpace(token.RefreshToken))
                    {
                        Logger.WriteDebug($"Refreshed access token: {token.AccessToken}");
                        Logger.WriteDebug($"Refresh token: {token.RefreshToken}");
                        SetRefreshToken(token.RefreshToken);
                        SetAccessToken(token.AccessToken);
                        SetStatus(ClientConnectionStatus.Connected);
                        SetupExpiry(token.ExpiresInSeconds);
                    }
                    else
                    {
                        Logger.WriteWarning("The login process returned a response from the oauth service with either an access token or refresh token missing - disconnecting.");
                        Logger.WriteDebug($"Refreshed access token: {token.AccessToken}");
                        Logger.WriteDebug($"Refresh token: {token.RefreshToken}");
                        SetStatus(ClientConnectionStatus.Disconnected);
                    }
                }
                else
                {
                    Logger.WriteWarning("The login process returned no response from the oauth service - disconnecting.");
                    SetStatus(ClientConnectionStatus.Disconnected);
                }
            }
        }
        protected override async Task OnBeginLoginAsync()
        {
            if (!string.IsNullOrWhiteSpace(RefreshToken))
            {
                Logger.WriteInfo($"Discovered refresh token...reconnecting to service at url {ServiceUri}...");
                await OnRefreshAccessTokenAsync();
            }
            else
            {
                Logger.WriteInfo($"Connecting to service at url {ServiceUri} as non-interactive client {ClientId})");
                var uri     = new Uri(ServiceUri, "oauth/token");
                var request = new FormServiceRequest <OAuthTokenResult>(uri, HttpVerb.Post);
                var data    = new OAuthAccessTokenRequestData()
                {
                    ClientId = ClientId, ClientSecret = ClientSecret, GrantType = GrantTypes.ClientSecret
                };
                var token = await request.ExecuteAsync(data.AsDictionary());

                if (token != null)
                {
                    SetRefreshToken(token.RefreshToken);
                    SetAccessToken(token.AccessToken);

                    if (!string.IsNullOrWhiteSpace(AccessToken))
                    {
                        SetStatus(ClientConnectionStatus.Connected);
                        SetupExpiry(token.ExpiresInSeconds);
                    }
                    else
                    {
                        SetStatus(ClientConnectionStatus.Disconnected);
                    }
                }
                else
                {
                    Logger.WriteWarning("The login process returned no response from the oauth service.");
                    SetStatus(ClientConnectionStatus.Disconnected);
                }
            }
        }