public async Task <ActionResult> OpenId(string code) { if (string.IsNullOrWhiteSpace(code)) { throw new ArgumentNullException(nameof(code)); } var authenticatedUser = await this.GetAuthenticatedUser(Constants.CookieName); var request = _dataProtector.Unprotect <AuthorizationRequest>(code); var actionResult = await _authenticateActions.AuthenticateResourceOwnerOpenId( request.ToParameter(), authenticatedUser, code); var result = this.CreateRedirectionFromActionResult(actionResult, request); if (result != null) { return(result); } var viewModel = new LoginOpenIdViewModel { Code = code }; return(View(viewModel)); }
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 static LocalAuthenticationParameter ToParameter(this LoginOpenIdViewModel viewModel) { if (viewModel == null) { throw new ArgumentNullException(nameof(viewModel)); } return(new LocalAuthenticationParameter { Password = viewModel.Password, UserName = viewModel.UserName }); }
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)); }