Exemple #1
0
        public async Task When_Passing_Null_LocalAuthenticateParameter_To_The_Action_LocalUserAuthentication_Then_Exception_Is_Thrown()
        {
            // ARRANGE
            InitializeFakeObjects();
            var localAuthenticationParameter = new LocalAuthenticationParameter();

            // ACT & ASSERT
            await Assert.ThrowsAsync <ArgumentNullException>(() => _authenticateActions.LocalOpenIdUserAuthentication(null, null, null, null));

            await Assert.ThrowsAsync <ArgumentNullException>(() => _authenticateActions.LocalOpenIdUserAuthentication(localAuthenticationParameter, null, null, null));
        }
Exemple #2
0
        public async Task <ActionResult> OpenId(LoginOpenIdViewModel loginViewModel)
        {
            if (loginViewModel == null)
            {
                throw new ArgumentNullException(nameof(loginViewModel));
            }

            if (string.IsNullOrWhiteSpace(loginViewModel.Code))
            {
                throw new ArgumentNullException(nameof(loginViewModel.Code));
            }

            var uiLocales = DefaultLanguage;

            try
            {
                // 1. Decrypt the request
                var request = _dataProtector.Unprotect <AuthorizationRequest>(loginViewModel.Code);
                // 2. Retrieve the default language
                uiLocales = string.IsNullOrWhiteSpace(request.UiLocales) ? DefaultLanguage : request.UiLocales;
                // 3. Check the state of the view model
                if (!ModelState.IsValid)
                {
                    return(View("OpenId", loginViewModel));
                }

                // 4. Local authentication
                var actionResult = await _authenticateActions.LocalOpenIdUserAuthentication(new LocalAuthenticationParameter
                {
                    Password = loginViewModel.Password,
                    UserName = loginViewModel.CardNumber
                },
                                                                                            request.ToParameter(),
                                                                                            loginViewModel.Code);

                var subject = actionResult.Claims.First(c => c.Type == Core.Jwt.Constants.StandardResourceOwnerClaimNames.Subject).Value;

                // 5. Authenticate the user by adding a cookie
                var authenticationManager = this.GetAuthenticationManager();
                await SetLocalCookie(authenticationManager, actionResult.Claims);

                // 6. Redirect the user agent
                var result = this.CreateRedirectionFromActionResult(actionResult.ActionResult,
                                                                    request);
                if (result != null)
                {
                    return(result);
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("invalid_credentials", ex.Message);
            }

            // TranslateView(uiLocales);
            return(View("OpenId", loginViewModel));
        }
        public async Task <ActionResult> LocalLoginOpenId(LoginOpenIdViewModel viewModel)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException(nameof(viewModel));
            }

            if (string.IsNullOrWhiteSpace(viewModel.Code))
            {
                throw new ArgumentNullException(nameof(viewModel.Code));
            }

            try
            {
                // 1. Decrypt the request
                var request = _dataProtector.Unprotect <AuthorizationRequest>(viewModel.Code);

                // 2. Check the state of the view model
                if (!ModelState.IsValid)
                {
                    return(View("OpenId", viewModel));
                }

                // 3. Local authentication
                var actionResult = await _authenticateActions.LocalOpenIdUserAuthentication(viewModel.ToParameter(),
                                                                                            request.ToParameter(),
                                                                                            viewModel.Code);

                var subject = actionResult.Claims.First(c => c.Type == SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.Subject).Value;

                // 4. Authenticate the user by adding a cookie
                var authenticationManager = this.GetAuthenticationManager();
                await SetLocalCookie(authenticationManager, actionResult.Claims);

                // 5. Redirect the user agent
                var result = this.CreateRedirectionFromActionResult(actionResult.ActionResult,
                                                                    request);
                if (result != null)
                {
                    return(result);
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("invalid_credentials", ex.Message);
            }

            return(View("OpenId", viewModel));
        }
Exemple #4
0
        public async Task <ActionResult> LocalLoginOpenId(AuthorizeOpenIdViewModel authorizeOpenId)
        {
            if (authorizeOpenId == null)
            {
                throw new ArgumentNullException(nameof(authorizeOpenId));
            }

            if (string.IsNullOrWhiteSpace(authorizeOpenId.Code))
            {
                throw new ArgumentNullException(nameof(authorizeOpenId.Code));
            }

            await SetUser();

            var uiLocales = DefaultLanguage;

            try
            {
                // 1. Decrypt the request
                var request = _dataProtector.Unprotect <AuthorizationRequest>(authorizeOpenId.Code);

                // 2. Retrieve the default language
                uiLocales = string.IsNullOrWhiteSpace(request.UiLocales) ? DefaultLanguage : request.UiLocales;

                // 3. Check the state of the view model
                if (!ModelState.IsValid)
                {
                    await TranslateView(uiLocales);
                    await SetIdProviders(authorizeOpenId);

                    return(View("OpenId", authorizeOpenId));
                }

                // 4. Local authentication
                var actionResult = await _authenticateActions.LocalOpenIdUserAuthentication(authorizeOpenId.ToParameter(),
                                                                                            request.ToParameter(),
                                                                                            authorizeOpenId.Code);

                var subject = actionResult.Claims.First(c => c.Type == SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.Subject).Value;

                // 5. Authenticate the user by adding a cookie
                await SetLocalCookie(actionResult.Claims, request.SessionId);

                _simpleIdentityServerEventSource.AuthenticateResourceOwner(subject);

                // 6. Redirect the user agent
                var result = this.CreateRedirectionFromActionResult(actionResult.ActionResult,
                                                                    request);
                if (result != null)
                {
                    LogAuthenticateUser(actionResult.ActionResult, request.ProcessId);
                    return(result);
                }
            }
            catch (Exception ex)
            {
                _simpleIdentityServerEventSource.Failure(ex.Message);
                ModelState.AddModelError("invalid_credentials", ex.Message);
            }

            await TranslateView(uiLocales);
            await SetIdProviders(authorizeOpenId);

            return(View("OpenId", authorizeOpenId));
        }