コード例 #1
0
        public async Task AuthenticateUserAsync(string serviceResourceId, string userId = null)
        {
            if (string.IsNullOrEmpty(serviceResourceId))
            {
                throw new ServiceException(
                          new Error
                {
                    Code    = OAuthConstants.ErrorCodes.AuthenticationFailure,
                    Message = "Service resource ID is required to authenticate a user with AuthenticateUserAsync."
                });
            }

            this.currentServiceResourceId = serviceResourceId;

            IAuthenticationResult authenticationResult = null;

            try
            {
                authenticationResult = await this.AuthenticateUserSilently(serviceResourceId, userId, false).ConfigureAwait(false);

                this.ValidateAuthenticationResult(authenticationResult);
            }
            catch (Exception)
            {
                // If silent authentication fails swallow the exception and try prompting the user.
                // Reset authenticationResult to null in case we have a failed result object.
                authenticationResult = null;
            }

            if (authenticationResult == null)
            {
                if (string.IsNullOrEmpty(returnUrl))
                {
                    throw new ServiceException(
                              new Error
                    {
                        Code    = OAuthConstants.ErrorCodes.AuthenticationFailure,
                        Message = "The user could not be silently authenticated and return URL is required to prompt the user for authentication."
                    });
                }

                try
                {
                    authenticationResult = await this.AuthenticateUser(serviceResourceId, userId).ConfigureAwait(false);
                }
                catch (Exception exception)
                {
                    BusinessAuthenticationExceptionHelper.HandleAuthenticationException(exception);
                }

                if (authenticationResult == null)
                {
                    BusinessAuthenticationExceptionHelper.HandleAuthenticationException(null);
                }
            }

            this.CurrentAccountSession = this.ConvertAuthenticationResultToAccountSession(authenticationResult);
        }
コード例 #2
0
        public override async Task AuthenticateUserWithRefreshTokenAsync(string refreshToken, string serviceResourceId)
        {
            if (string.IsNullOrEmpty(refreshToken))
            {
                throw new ServiceException(
                          new Error
                {
                    Code    = OAuthConstants.ErrorCodes.AuthenticationFailure,
                    Message = "Refresh token is required to authenticate a user with a refresh token."
                });
            }

            this.currentServiceResourceId = serviceResourceId;

            IAuthenticationResult authenticationResult = null;

            try
            {
                if (this.clientCertificate != null)
                {
                    var clientAssertionCertificate = new ClientAssertionCertificate(this.clientId, this.clientCertificate);
                    authenticationResult = await this.authenticationContextWrapper.AcquireTokenByRefreshTokenAsync(
                        refreshToken,
                        clientAssertionCertificate,
                        serviceResourceId).ConfigureAwait(false);
                }
                else if (!string.IsNullOrEmpty(this.clientSecret))
                {
                    var clientCredential = this.GetClientCredentialForAuthentication(this.clientId, this.clientSecret);
                    authenticationResult = await this.authenticationContextWrapper.AcquireTokenByRefreshTokenAsync(
                        refreshToken,
                        clientCredential,
                        serviceResourceId).ConfigureAwait(false);
                }
                else
                {
                    authenticationResult = await this.authenticationContextWrapper.AcquireTokenByRefreshTokenAsync(
                        refreshToken,
                        this.clientId,
                        serviceResourceId).ConfigureAwait(false);
                }
            }
            catch (Exception exception)
            {
                BusinessAuthenticationExceptionHelper.HandleAuthenticationException(exception);
            }

            if (authenticationResult == null)
            {
                BusinessAuthenticationExceptionHelper.HandleAuthenticationException(null);
            }

            this.CurrentAccountSession = this.ConvertAuthenticationResultToAccountSession(authenticationResult);
        }
コード例 #3
0
        public async Task AuthenticateUserWithAuthorizationCodeAsync(string authorizationCode, string serviceResourceId)
        {
            if (string.IsNullOrEmpty(authorizationCode))
            {
                throw new ServiceException(
                          new Error
                {
                    Code    = OAuthConstants.ErrorCodes.AuthenticationFailure,
                    Message = "Authorization code is required to authenticate a user with an authorization code."
                });
            }

            if (string.IsNullOrEmpty(returnUrl))
            {
                throw new ServiceException(
                          new Error
                {
                    Code    = OAuthConstants.ErrorCodes.AuthenticationFailure,
                    Message = "Return URL is required to authenticate a user with an authorization code."
                });
            }

            this.currentServiceResourceId = serviceResourceId;

            IAuthenticationResult authenticationResult = null;

            try
            {
                if (this.clientCertificate != null)
                {
                    var clientAssertionCertificate = new ClientAssertionCertificate(this.clientId, this.clientCertificate);
                    authenticationResult = await this.authenticationContextWrapper.AcquireTokenByAuthorizationCodeAsync(
                        authorizationCode,
                        new Uri(this.returnUrl),
                        clientAssertionCertificate,
                        serviceResourceId).ConfigureAwait(false);
                }
                else if (!string.IsNullOrEmpty(this.clientSecret))
                {
                    var clientCredential = this.GetClientCredentialForAuthentication(this.clientId, this.clientSecret);
                    authenticationResult = await this.authenticationContextWrapper.AcquireTokenByAuthorizationCodeAsync(
                        authorizationCode,
                        new Uri(this.returnUrl),
                        clientCredential,
                        serviceResourceId).ConfigureAwait(false);
                }
                else
                {
                    throw new ServiceException(
                              new Error
                    {
                        Code    = OAuthConstants.ErrorCodes.AuthenticationFailure,
                        Message = "Client certificate or client secret is required to authenticate a user with an authorization code."
                    });
                }
            }
            catch (Exception exception)
            {
                BusinessAuthenticationExceptionHelper.HandleAuthenticationException(exception);
            }

            if (authenticationResult == null)
            {
                BusinessAuthenticationExceptionHelper.HandleAuthenticationException(null);
            }

            this.CurrentAccountSession = this.ConvertAuthenticationResultToAccountSession(authenticationResult);
        }