/// <inheritdoc/> public async Task <bool> LoginAsync() { _isConnected = false; if (!_isInitialized) { throw new InvalidOperationException("Microsoft OneDrive not initialized."); } OneDriveAuthenticationHelper.ResourceUri = "https://api.onedrive.com/v1.0"; if (_accountProviderType == AccountProviderType.Msa) { _accountProvider = OneDriveAuthenticationHelper.CreateMSAAuthenticationProvider(_appClientId, _scopes); await OneDriveAuthenticationHelper.AuthenticateMsaUserAsync(); } else if (_accountProviderType == AccountProviderType.OnlineId) { _accountProvider = new OnlineIdAuthenticationProvider(_scopes); await((OnlineIdAuthenticationProvider)_accountProvider).RestoreMostRecentFromCacheOrAuthenticateUserAsync(); } _oneDriveProvider = new OneDriveClient(OneDriveAuthenticationHelper.ResourceUri, _accountProvider); _isConnected = true; return(_isConnected); }
/// <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); }
/// <summary> /// Create an ADAL Authentication provider /// </summary> /// <param name="appClientId">client application id</param> /// <returns>an authentication provider for Azure Active Directory</returns> internal static IAuthenticationProvider CreateAdalAuthenticationProvider(string appClientId) { _appClientId = appClientId; _accountProvider = new DelegateAuthenticationProvider( async(requestMessage) => { requestMessage.Headers.Authorization = new AuthenticationHeaderValue( "bearer", await OneDriveAuthenticationHelper.AuthenticateAdalUserAsync().ConfigureAwait(false)); return; }); return(_accountProvider); }