public static LocalAuthenticateParameter ToParameter(this EidAuthorizeViewModel viewModel) { if (viewModel == null) { throw new ArgumentNullException(nameof(viewModel)); } return(new LocalAuthenticateParameter { Xml = viewModel.Xml }); }
private async Task SetIdProviders(EidAuthorizeViewModel authorizeViewModel) { var schemes = (await _authenticationSchemeProvider.GetAllSchemesAsync()).Where(p => !string.IsNullOrWhiteSpace(p.DisplayName)); var idProviders = new List <IdProviderViewModel>(); foreach (var scheme in schemes) { idProviders.Add(new IdProviderViewModel { AuthenticationScheme = scheme.Name, DisplayName = scheme.DisplayName }); } authorizeViewModel.EidUrl = _eidAuthenticateOptions.EidUrl; authorizeViewModel.IdProviders = idProviders; }
public async Task <ActionResult> OpenId(string code) { if (string.IsNullOrWhiteSpace(code)) { throw new ArgumentNullException(nameof(code)); } var authenticatedUser = await SetUser(); var request = _dataProtector.Unprotect <AuthorizationRequest>(code); var actionResult = await _authenticateActions.AuthenticateResourceOwnerOpenId( request.ToParameter(), authenticatedUser.Key, code); var result = this.CreateRedirectionFromActionResult(actionResult, request); if (result != null) { await LogAuthenticateUser(actionResult, request.ProcessId); return(result); } await TranslateView(request.UiLocales); var viewModel = new EidAuthorizeViewModel { Code = code }; await SetIdProviders(viewModel); return(View(viewModel)); }
public async Task <ActionResult> LocalLoginOpenId(EidAuthorizeViewModel 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 _loginActions.OpenIdLocalAuthenticate(authorizeOpenId.ToParameter(), request.ToParameter(), authorizeOpenId.Code, _eidAuthenticateOptions.ImagePath, Request.GetAbsoluteUriWithVirtualPath()); 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) { await 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)); }