public async Task <ActionResult> LocalLogin(AuthorizeViewModel authorizeViewModel)
        {
            var authenticatedUser = await this.GetAuthenticatedUser(Constants.CookieName);

            if (authenticatedUser != null &&
                authenticatedUser.Identity != null &&
                authenticatedUser.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "User"));
            }

            if (authorizeViewModel == null)
            {
                throw new ArgumentNullException(nameof(authorizeViewModel));
            }

            if (!ModelState.IsValid)
            {
                await TranslateView(DefaultLanguage);

                return(View("Index", authorizeViewModel));
            }

            try
            {
                var resourceOwner = await _authenticateActions.LocalUserAuthentication(authorizeViewModel.ToParameter());

                var claims = resourceOwner.Claims;
                claims.Add(new Claim(ClaimTypes.AuthenticationInstant,
                                     DateTimeOffset.UtcNow.ConvertToUnixTimestamp().ToString(CultureInfo.InvariantCulture),
                                     ClaimValueTypes.Integer));
                var subject = claims.First(c => c.Type == Core.Jwt.Constants.StandardResourceOwnerClaimNames.Subject).Value;
                var authenticationManager = this.GetAuthenticationManager();
                if (resourceOwner.TwoFactorAuthentication == Core.Models.TwoFactorAuthentications.NONE)
                {
                    await SetLocalCookie(authenticationManager, claims);

                    _simpleIdentityServerEventSource.AuthenticateResourceOwner(subject);
                    return(RedirectToAction("Index", "User"));
                }

                // 2.1 Store temporary information in cookie
                await SetTwoFactorCookie(authenticationManager, claims);

                // 2.2. Send confirmation code
                var code = await _authenticateActions.GenerateAndSendCode(subject);

                _simpleIdentityServerEventSource.GetConfirmationCode(code);
                return(RedirectToAction("SendCode"));
            }
            catch (Exception exception)
            {
                _simpleIdentityServerEventSource.Failure(exception.Message);
                await TranslateView("en");

                ModelState.AddModelError("invalid_credentials", exception.Message);
                return(View("Index", authorizeViewModel));
            }
        }
Пример #2
0
        public async Task <ActionResult> LocalLogin(LoginViewModel loginViewModel)
        {
            var authenticatedUser = await SetUser();

            if (authenticatedUser.Key != null &&
                authenticatedUser.Key.Identity != null &&
                authenticatedUser.Key.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "User"));
            }

            if (loginViewModel == null)
            {
                throw new ArgumentNullException(nameof(loginViewModel));
            }

            if (!ModelState.IsValid)
            {
                await TranslateView(DefaultLanguage);
                await SetIdProviders(loginViewModel);

                return(View("Index", loginViewModel));
            }

            try
            {
                var resourceOwner = await _loginActions.LocalAuthenticate(loginViewModel.ToParameter(), _eidAuthenticateOptions.ImagePath, Request.GetAbsoluteUriWithVirtualPath());

                var claims = resourceOwner.Claims;
                claims.Add(new Claim(ClaimTypes.AuthenticationInstant,
                                     DateTimeOffset.UtcNow.ConvertToUnixTimestamp().ToString(CultureInfo.InvariantCulture),
                                     ClaimValueTypes.Integer));
                var subject = claims.First(c => c.Type == SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.Subject).Value;
                await SetLocalCookie(claims, Guid.NewGuid().ToString());

                _simpleIdentityServerEventSource.AuthenticateResourceOwner(subject);
                return(RedirectToAction("Index", "User"));
            }
            catch (Exception exception)
            {
                _simpleIdentityServerEventSource.Failure(exception.Message);
                await TranslateView("en");

                ModelState.AddModelError("invalid_credentials", exception.Message);
                await SetIdProviders(loginViewModel);

                return(View("Index", loginViewModel));
            }
        }