/// <summary>
        /// Logout the current user
        /// </summary>
        /// <returns>success or failure</returns>
        public bool Logout()
        {
            if (!_isInitialized)
            {
                throw new InvalidOperationException("Microsoft Graph not initialized.");
            }

            _authentication = null;

            return(true);
        }
        /// <summary>
        /// Login the user from Azure AD and Get Microsoft Graph access token.
        /// </summary>
        /// <remarks>Need Sign in and read user profile scopes (User.Read)</remarks>
        /// <returns>Returns success or failure of login attempt.</returns>
        public async Task <bool> LoginAsync()
        {
            _isConnected = false;
            if (!_isInitialized)
            {
                throw new InvalidOperationException("Microsoft Graph not initialized.");
            }

            _authentication = new MicrosoftGraphAuthenticationHelper();
            string accessToken = null;

            if (_authenticationModel == AuthenticationModel.V1)
            {
                accessToken = await _authentication.GetUserTokenAsync(_appClientId);
            }
            else
            {
                // accessToken = await _authentication.GetUserTokenV2Async(_appClientId);
            }

            if (string.IsNullOrEmpty(accessToken))
            {
                return(_isConnected);
            }

            _isConnected = true;

            _user = new MicrosoftGraphUserService(_graphProvider);

            if ((_servicesToInitialize & ServicesToInitialize.UserProfile) == ServicesToInitialize.UserProfile)
            {
                await GetUserAsyncProfile();
            }

            // if ((_servicesToInitialize & ServicesToInitialize.OneDrive) == ServicesToInitialize.OneDrive)
            // {
            //    _user.InitializeDrive();
            // }
            if ((_servicesToInitialize & ServicesToInitialize.Message) == ServicesToInitialize.Message)
            {
                _user.InitializeMessage();
            }

            if ((_servicesToInitialize & ServicesToInitialize.Event) == ServicesToInitialize.Event)
            {
                _user.InitializeEvent();
            }

            return(_isConnected);
        }
示例#3
0
        /// <summary>
        /// Login the user from Azure AD and Get Microsoft Graph access token.
        /// </summary>
        /// <remarks>Need Sign in and read user profile scopes (User.Read)</remarks>
        /// <param name="loginHint">UPN for user - avoids realm discovery prompt</param>
        /// <returns>Returns success or failure of login attempt.</returns>
        public async Task <bool> LoginAsync(string loginHint)
        {
            IsConnected = false;
            if (!IsInitialized)
            {
                throw new InvalidOperationException("Microsoft Graph not initialized.");
            }

            Authentication = new MicrosoftGraphAuthenticationHelper(DelegatedPermissionScopes);
            string accessToken = null;

            if (AuthenticationModel == Toolkit.Services.MicrosoftGraph.MicrosoftGraphEnums.AuthenticationModel.V1)
            {
                accessToken = await((MicrosoftGraphAuthenticationHelper)Authentication).GetUserTokenAsync(AppClientId);
            }
            else
            {
                accessToken = await Authentication.GetUserTokenV2Async(AppClientId, loginHint);
            }

            if (string.IsNullOrEmpty(accessToken))
            {
                return(IsConnected);
            }

            IsConnected = true;

            User = new MicrosoftGraphUserService(GraphProvider);

            if ((ServicesToInitialize & Toolkit.Services.MicrosoftGraph.MicrosoftGraphEnums.ServicesToInitialize.UserProfile) == Toolkit.Services.MicrosoftGraph.MicrosoftGraphEnums.ServicesToInitialize.UserProfile)
            {
                await GetUserAsyncProfile();
            }

            // if ((_servicesToInitialize & ServicesToInitialize.OneDrive) == ServicesToInitialize.OneDrive)
            // {
            //    _user.InitializeDrive();
            // }
            if ((ServicesToInitialize & Toolkit.Services.MicrosoftGraph.MicrosoftGraphEnums.ServicesToInitialize.Message) == Toolkit.Services.MicrosoftGraph.MicrosoftGraphEnums.ServicesToInitialize.Message)
            {
                User.InitializeMessage();
            }

            if ((ServicesToInitialize & Toolkit.Services.MicrosoftGraph.MicrosoftGraphEnums.ServicesToInitialize.Event) == Toolkit.Services.MicrosoftGraph.MicrosoftGraphEnums.ServicesToInitialize.Event)
            {
                User.InitializeEvent();
            }

            return(IsConnected);
        }
        /// <summary>
        /// Login the user from Azure AD and Get Microsoft Graph access token.
        /// </summary>
        /// <remarks>Need Sign in and read user profile scopes (User.Read)</remarks>
        /// <returns>Returns success or failure of login attempt.</returns>
        public async Task <bool> LoginAsync()
        {
            _isConnected = false;
            if (!_isInitialized)
            {
                throw new InvalidOperationException("Microsoft Graph not initialized.");
            }

            _authentication = new MicrosoftGraphAuthenticationHelper();
            var accessToken = await _authentication.GetUserTokenAsync(_appClientId);

            if (string.IsNullOrEmpty(accessToken))
            {
                return(_isConnected);
            }

            _isConnected = true;

            await InitializeUserAsync();

            return(_isConnected);
        }