示例#1
0
        // public OneDriveService inse = Microsoft.Toolkit.Services.OneDrive.OneDriveService.Instance.Initialize( appClientId:OneDriveService., scopes, null, null);

        private async void btn_Login_Click(object sender, RoutedEventArgs e)
        {
            if (this.oneDriveClient == null)
            {
                try
                {
                    // Setting up the client here, passing in our Client Id, Return Url,
                    // Scopes that we want permission to, and building a Web Broker to
                    // go do our authentication.



                    this.oneDriveClient = await OneDriveClient.GetAuthenticatedMicrosoftAccountClient(
                        clientId,
                        returnUrl,
                        scopes,
                        webAuthenticationUi : new WebAuthenticationBrokerWebAuthenticationUi());



                    // Show in text box that we are connected.
                    txtBox_Response.Text = "We are now connected";

                    // We are either just autheticated and connected or we already connected,
                    // either way we need the drive button now.
                    btn_GetDriveId.Visibility = Visibility.Visible;
                }
                catch (OneDriveException exception)
                {
                    // Eating the authentication cancelled exceptions and resetting our client.
                    if (!exception.IsMatch(OneDriveErrorCode.AuthenticationCancelled.ToString()))
                    {
                        if (exception.IsMatch(OneDriveErrorCode.AuthenticationFailure.ToString()))
                        {
                            txtBox_Response.Text = "Authentication failed/cancelled, disposing of the client...";

                            ((OneDriveClient)this.oneDriveClient).Dispose();
                            this.oneDriveClient = null;
                        }
                        else
                        {
                            // Or we failed due to someother reason, let get that exception printed out.
                            txtBox_Response.Text = exception.Error.ToString();
                        }
                    }
                    else
                    {
                        ((OneDriveClient)this.oneDriveClient).Dispose();
                        this.oneDriveClient = null;
                    }
                }
            }
        }
        public async Task GetAuthenticatedMicrosoftAccountClient_NoSecret()
        {
            var appId     = "appId";
            var returnUrl = "returnUrl";
            var scopes    = new string[] { "scope" };

            this.SetupServiceInfoProviderForMicrosoftAccount(appId, /* clientSecret */ null, returnUrl, scopes);

            var client = await OneDriveClient.GetAuthenticatedMicrosoftAccountClient(
                appId,
                returnUrl,
                scopes,
                this.serviceInfoProvider.Object,
                this.credentialCache.Object,
                this.httpProvider.Object);

            this.authenticationProvider.Verify(provider => provider.AuthenticateAsync(), Times.Once);
        }
        /// <summary>
        /// Gets the client.
        /// </summary>
        /// <returns></returns>
        private static async Task <IOneDriveClient> GetClient()
        {
            CredentialCache credentialCache = new CredentialCache();
            string          credentials     = OneDriveCloudStorageServiceSettings.Default.Credentials;

            if (!String.IsNullOrWhiteSpace(credentials))
            {
                credentialCache.InitializeCacheFromBlob(Encoding.Default.GetBytes(credentials));
            }

            IServiceInfoProvider serviceInfoProvider = new ServiceInfoProvider(new FormsWebAuthenticationUi())
            {
                UserSignInName = OneDriveCloudStorageServiceSettings.Default.UserId
            };

            return(await OneDriveClient.GetAuthenticatedMicrosoftAccountClient(
                       Util.Decrypt(OneDriveCloudStorageServiceSettings.Default.AppKey, CultureConstants.InvariantCulture.NativeName),
                       RedirectUri, s_scopes, serviceInfoProvider, credentialCache).ConfigureAwait(false));
        }
示例#4
0
        public static async Task <IOneDriveClient> LoginAsync(string account, string code, string settingsPassPhrase)
        {
            if (string.IsNullOrEmpty(account))
            {
                throw new ArgumentNullException(nameof(account));
            }

            var refreshToken = LoadRefreshToken(account, settingsPassPhrase);

            IOneDriveClient client;

            if (!string.IsNullOrEmpty(refreshToken))
            {
                client = await OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(Secrets.CLIENT_ID, LIVE_LOGIN_DESKTOP_URI, scopes, Secrets.CLIENT_SECRET, refreshToken);
            }
            else
            {
                if (string.IsNullOrEmpty(code))
                {
                    client = await OneDriveClient.GetAuthenticatedMicrosoftAccountClient(Secrets.CLIENT_ID, LIVE_LOGIN_DESKTOP_URI, scopes, Secrets.CLIENT_SECRET, new WebAuthenticationUi(account));
                }
                else
                {
                    client = OneDriveClient.GetMicrosoftAccountClient(Secrets.CLIENT_ID, LIVE_LOGIN_DESKTOP_URI, scopes, Secrets.CLIENT_SECRET);
                    client.AuthenticationProvider.CurrentAccountSession = new AccountSession()
                    {
                        AccessToken = code
                    };
                }

                await client.AuthenticateAsync();

                if (!client.IsAuthenticated)
                {
                    throw new AuthenticationException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.RetrieveAuthenticationCodeFromUri, LIVE_LOGIN_AUTHORIZE_URI));
                }
            }

            SaveRefreshToken(account, client.AuthenticationProvider.CurrentAccountSession.RefreshToken, settingsPassPhrase);

            return(client);
        }