Exemplo n.º 1
0
        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));
        }