コード例 #1
0
        /// <summary>
        /// Signs in the user
        /// </summary>
        /// <returns>Returns success or failure of login attempt.</returns>
        public override async Task <bool> LoginAsync()
        {
            IsConnected = false;

            if (!IsInitialized)
            {
                throw new InvalidOperationException("Microsoft OneDrive not initialized.");
            }

            string resourceEndpointUri = null;

            if (_accountProviderType == AccountProviderType.Adal)
            {
                DiscoveryService  discoveryService  = null;
                DiscoverySettings discoverySettings = DiscoverySettings.Load();

                if (discoverySettings == null)
                {
                    // For OneDrive for business only
                    var authDiscoveryResult = await OneDriveAuthenticationHelper.AuthenticateAdalUserForDiscoveryAsync(AppClientId);

                    discoveryService = await OneDriveAuthenticationHelper.GetUserServiceResource(authDiscoveryResult);

                    discoverySettings = new DiscoverySettings {
                        ServiceEndpointUri = discoveryService.ServiceEndpointUri, ServiceResourceId = discoveryService.ServiceResourceId
                    };
                    discoverySettings.Save();
                }

                OneDriveAuthenticationHelper.ResourceUri = discoverySettings.ServiceResourceId;
                _accountProvider = OneDriveAuthenticationHelper.CreateAdalAuthenticationProvider(AppClientId);
                await OneDriveAuthenticationHelper.AuthenticateAdalUserAsync(true);

                resourceEndpointUri = discoverySettings.ServiceEndpointUri;
            }
            else if (_accountProviderType == AccountProviderType.Msa)
            {
                OneDriveAuthenticationHelper.ResourceUri = "https://api.onedrive.com/v1.0";
                _accountProvider = OneDriveAuthenticationHelper.CreateMSAAuthenticationProvider(AppClientId, Scopes);

                await((MsaAuthenticationProvider)OneDriveAuthenticationHelper.AuthenticationProvider).RestoreMostRecentFromCacheOrAuthenticateUserAsync();
                resourceEndpointUri = OneDriveAuthenticationHelper.ResourceUri;
            }
            else if (_accountProviderType == AccountProviderType.OnlineId)
            {
                OneDriveAuthenticationHelper.ResourceUri = "https://api.onedrive.com/v1.0";
                _accountProvider = new OnlineIdAuthenticationProvider(Scopes);
                await((MsaAuthenticationProvider)_accountProvider).RestoreMostRecentFromCacheOrAuthenticateUserAsync();
                resourceEndpointUri = OneDriveAuthenticationHelper.ResourceUri;
            }

            _oneDriveProvider = new OneDriveClient(resourceEndpointUri, _accountProvider);

            IsConnected = true;
            return(IsConnected);
        }
コード例 #2
0
        /// <summary>
        /// Logout the current user
        /// </summary>
        /// <returns>success or failure</returns>
        public override async Task LogoutAsync()
        {
            if (!IsInitialized)
            {
                throw new InvalidOperationException("Microsoft OneDrive not initialized.");
            }

            if (_accountProvider != null)
            {
                if (_accountProviderType == AccountProviderType.OnlineId || _accountProviderType == AccountProviderType.Msa)
                {
                    await((MsaAuthenticationProvider)_accountProvider).SignOutAsync();
                }
                else if (_accountProviderType == AccountProviderType.Adal)
                {
                    OneDriveAuthenticationHelper.AzureAdContext.TokenCache.Clear();
                    DiscoverySettings.Clear();
                    UserInfoSettings.Clear();
                }
            }
        }