public async Task <bool> TryAuthenticateWithUiAsync(ICloudAuthentication authentication)
        {
            if (IsAuthenticated)
            {
                await SignOutAsync();
            }

            IsAuthenticated = !(await authentication.AuthenticateAsync("http://authenticationUri", "http://exceptedUi")).IsCanceled;
            return(IsAuthenticated);
        }
        /// <inheritdoc/>
        public async Task <bool> TryAuthenticateWithUiAsync(ICloudAuthentication authentication)
        {
            var authenticationUriString = $"https://login.live.com/oauth20_authorize.srf?" + $"client_id={SecurityHelper.ToUnsecureString(_clientId)}&redirect_uri={Uri.EscapeDataString(SecurityHelper.ToUnsecureString(_redirectUri))}&response_type=code&display=touch&scope={Uri.EscapeDataString(string.Join(" ", _scope))}";

            var authenticationResult = await authentication.AuthenticateAsync(authenticationUriString, SecurityHelper.ToUnsecureString(_redirectUri));

            if (authenticationResult.IsCanceled)
            {
                IsAuthenticated = false;
                return(false);
            }

            try
            {
                var code = authenticationResult.RedirectedUri?.ToString().Split('=').Last();

                using (var handler = new HttpClientHandler())
                {
                    handler.ClientCertificateOptions = ClientCertificateOption.Automatic;
                    handler.UseDefaultCredentials    = true;
                    handler.Credentials = System.Net.CredentialCache.DefaultCredentials;

                    using (var httpProvider = new HttpProvider(handler, true))
                    {
                        var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://login.live.com/oauth20_token.srf")
                        {
                            Content = new StringContent($"client_id={SecurityHelper.ToUnsecureString(_clientId)}&code={code}&grant_type=authorization_code&redirect_uri={SecurityHelper.ToUnsecureString(_redirectUri)}", Encoding.UTF8, "application/x-www-form-urlencoded")
                        };

                        var response = await httpProvider.SendAsync(createMessage);

                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            var json         = JObject.Parse(await response.Content.ReadAsStringAsync());
                            var refreshToken = json["refresh_token"]?.ToString();
                            TokenProvider.SetToken("RefreshToken", SecurityHelper.EncryptString(SecurityHelper.ToSecureString(refreshToken)));
                            await TryAuthenticateAsync();
                        }
                        else
                        {
                            IsAuthenticated = false;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                IsAuthenticated = false;
            }

            return(IsAuthenticated);
        }
Esempio n. 3
0
        /// <inheritdoc/>
        public async Task <bool> TryAuthenticateWithUiAsync(ICloudAuthentication authentication)
        {
            var oauth2State       = Guid.NewGuid().ToString("N");
            var authenticationUri = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Token, SecurityHelper.ToUnsecureString(_appKey), new Uri(SecurityHelper.ToUnsecureString(_redirectUri)), oauth2State);

            var authenticationResult = await authentication.AuthenticateAsync(authenticationUri.ToString(), SecurityHelper.ToUnsecureString(_redirectUri));

            if (authenticationResult.IsCanceled)
            {
                IsAuthenticated = false;
                return(IsAuthenticated);
            }

            var result = DropboxOAuth2Helper.ParseTokenFragment(authenticationResult.RedirectedUri);

            if (result.State != oauth2State)
            {
                IsAuthenticated = false;
                return(IsAuthenticated);
            }

            TokenProvider.SetToken("AccessToken", SecurityHelper.EncryptString(SecurityHelper.ToSecureString(result.AccessToken)));
            return(await TryAuthenticateAsync());
        }