Example #1
0
        public static string[] TransformScopes(OneDriveScopes scopes)
        {
            StringBuilder sb = new StringBuilder();

            if ((scopes & OneDriveScopes.AppFolder) == OneDriveScopes.AppFolder)
            {
                sb.Append("onedrive.appfolder,");
            }

            if ((scopes & OneDriveScopes.OfflineAccess) == OneDriveScopes.OfflineAccess)
            {
                sb.Append("offline_access,");
            }

            if ((scopes & OneDriveScopes.ReadOnly) == OneDriveScopes.ReadOnly)
            {
                sb.Append("onedrive.readonly,");
            }

            if ((scopes & OneDriveScopes.ReadWrite) == OneDriveScopes.ReadWrite)
            {
                sb.Append("onedrive.readwrite");
            }

            if ((scopes & OneDriveScopes.WlSignin) == OneDriveScopes.WlSignin)
            {
                sb.Append("wl.signin");
            }

            return(sb.ToString().Split(','));
        }
Example #2
0
        /// <summary>
        /// Initialize OneDrive Service only for OnlineId Provider
        /// </summary>
        /// <param name="onlineIdPromptType">Specify whether to prompt for credentials. Set to DoNotPrompt if calling from a background task.</param>
        /// <returns>Success or failure</returns>
        public bool Initialize(OnlineIdAuthenticationProvider.PromptType onlineIdPromptType)
        {
            OneDriveScopes scopes = OneDriveScopes.ReadWrite | OneDriveScopes.OfflineAccess;

            _onlineIdPromptType = onlineIdPromptType;
            return(Initialize(scopes, _onlineIdPromptType));
        }
        public static async Task<Microsoft.OneDrive.OneDriveClient> GetOneDriveClientAsync(string clientId, OneDriveScopes[] scopes, IRefreshTokenHandler refreshTokenHandler = null)
        {
            LiveAuthClient authClient = new LiveAuthClient(clientId, refreshTokenHandler);
            var authScopes = from s in scopes select OAuthScopeAttribute.OAuthScopeForEnumValue(s);

            LiveLoginResult loginResult = await authClient.IntializeAsync(authScopes);
            if (loginResult.Status == LiveConnectSessionStatus.Connected)
            {
                return new OneDriveClient(new LiveConnectClient(loginResult.Session));
            }

            string startUrl = authClient.GetLoginUrl(authScopes);
            string endUrl = OAuthDesktopEndPoint;
            FormMicrosoftAccountAuth authForm = new FormMicrosoftAccountAuth(startUrl, endUrl);

            DialogResult result = await authForm.ShowDialogAsync();
            if (DialogResult.OK == result)
            {
                return await OnAuthComplete(authClient, authForm.AuthResult);
            }

            return null;
        }
        /// <summary>
        /// Initialize OneDrive service
        /// </summary>
        /// <param name="appClientId">Application Id Client. Could be null if AccountProviderType.OnlineId is used</param>
        /// <param name="accountProviderType">Account Provider type.
        /// <para>AccountProviderType.OnlineId: If the user is signed into a Windows system with a Microsoft Account, this user will be used for authentication request. Need to associate the App to the store</para>
        /// <para>AccountProviderType.Msa: Authenticate the user with a Microsoft Account. You need to register your app https://apps.dev.microsoft.com in the SDK Live section</para>
        /// <para>AccountProviderType.Adal: Authenticate the user with a Office 365 Account. You need to register your in Azure Active Directory</para></param>
        /// <param name="scopes">Scopes represent various permission levels that an app can request from a user. Could be null if AccountProviderType.Adal is used </param>
        /// <remarks>If AccountProvider</remarks>
        /// <returns>Success or failure.</returns>
        public bool Initialize(string appClientId, AccountProviderType accountProviderType = AccountProviderType.OnlineId, OneDriveScopes scopes = OneDriveScopes.ReadWrite | OneDriveScopes.OfflineAccess)
        {
            if (accountProviderType != AccountProviderType.OnlineId && string.IsNullOrEmpty(appClientId))
            {
                throw new ArgumentNullException(nameof(appClientId));
            }

            AppClientId = appClientId;

            if (accountProviderType != AccountProviderType.Adal)
            {
                Scopes = OneDriveHelper.TransformScopes(scopes);
            }

            IsInitialized        = true;
            _accountProviderType = accountProviderType;
            return(true);
        }
 /// <summary>
 /// Initialize OneDrive Service only for OnlineId Provider
 /// </summary>
 /// <param name="scopes">Scopes represent various permission levels that an app can request from a user</param>
 /// <returns>Success or failure</returns>
 public bool Initialize(OneDriveScopes scopes = OneDriveScopes.ReadWrite | OneDriveScopes.OfflineAccess)
 {
     return(Initialize(null, AccountProviderType.OnlineId, scopes));
 }
Example #6
0
        /// <inheritdoc/>
        public bool Initialize(string appClientId, AccountProviderType accountProviderType = AccountProviderType.OnlineId, OneDriveScopes scopes = OneDriveScopes.OfflineAccess | OneDriveScopes.ReadWrite)
        {
            if (accountProviderType == AccountProviderType.Adal || accountProviderType == AccountProviderType.Msal)
            {
                _instance = new GraphOneDriveService();
                return(_instance.Initialize(appClientId, accountProviderType, scopes));
            }

            if (accountProviderType != AccountProviderType.OnlineId && string.IsNullOrEmpty(appClientId))
            {
                throw new ArgumentNullException(nameof(appClientId));
            }

            _appClientId = appClientId;

            if (accountProviderType == AccountProviderType.Msa)
            {
                _scopes = OneDriveHelper.TransformScopes(scopes);
            }

            _isInitialized       = true;
            _accountProviderType = accountProviderType;
            return(true);
        }
Example #7
0
        /// <inheritdoc/>
        public bool Initialize(string appClientId, OneDriveEnums.AccountProviderType accountProviderType = AccountProviderType.Msal, OneDriveScopes scopes = OneDriveScopes.OfflineAccess | OneDriveScopes.ReadWrite)
        {
            if (accountProviderType == AccountProviderType.OnlineId || accountProviderType == AccountProviderType.Msa)
            {
                throw new ArgumentException("Authentication with OnlineId or Msa are not supported");
            }

            if (string.IsNullOrEmpty(appClientId))
            {
                throw new ArgumentNullException(nameof(appClientId));
            }

            _appClientId = appClientId;

            if (accountProviderType == AccountProviderType.Msal)
            {
                _scopes = new string[] { "https://graph.microsoft.com/Files.ReadWrite" };
            }

            _isInitialized       = true;
            _accountProviderType = accountProviderType;
            return(true);
        }
Example #8
0
 /// <inheritdoc/>
 public bool Initialize(OneDriveScopes scopes = OneDriveScopes.OfflineAccess | OneDriveScopes.ReadWrite)
 {
     throw new NotImplementedException();
 }
Example #9
0
 /// <summary>
 /// Initialize OneDrive Service only for OnlineId Provider
 /// </summary>
 /// <param name="scopes">Scopes represent various permission levels that an app can request from a user</param>
 /// <param name="onlineIdPromptType">Specify whether to prompt for credentials. Set to DoNotPrompt if calling from a background task.</param>
 /// <returns>Success or failure</returns>
 public bool Initialize(OneDriveScopes scopes = OneDriveScopes.ReadWrite | OneDriveScopes.OfflineAccess, OnlineIdAuthenticationProvider.PromptType onlineIdPromptType = OnlineIdAuthenticationProvider.PromptType.PromptIfNeeded)
 {
     _onlineIdPromptType = onlineIdPromptType;
     return(Initialize(null, AccountProviderType.OnlineId, scopes, _onlineIdPromptType));
 }