Example #1
0
        /// <summary>
        /// Get the current user id.
        /// </summary>
        public static Guid GetUserId(this IPrincipal principal)
        {
            var subject = principal.GetSubjectId();

            Guid.TryParse(subject, out Guid userId);
            return(userId);
        }
        /// <summary>
        /// Get the current user id.
        /// </summary>
        public static int GetUserId(this IPrincipal principal)
        {
            int userId;
            var subject = principal.GetSubjectId();

            int.TryParse(subject, out userId);
            return(userId);
        }
        public async Task <IActionResult> AdditionalAuthenticationFactor(
            AdditionalAuthenticationFactorViewModel model)
        {
            if (ModelState.IsValid)
            {
                // read identity from the temporary cookie
                //var info = await HttpContext.Authentication.GetAuthenticateInfoAsync(IdentityServerConstants.DefaultCookieAuthenticationScheme + ".2FA");
                IPrincipal tempUser = null;// info?.Principal;
                if (tempUser == null)
                {
                    throw new Exception("2FA error");
                }

                var user = _userRepository.GetUserBySubjectId(tempUser.GetSubjectId());

                // ... check code for user
                if (model.Code != "123")
                {
                    ModelState.AddModelError("code", "2FA code is invalid.");
                    return(View(model));
                }

                // login the user
                AuthenticationProperties props = null;
                if (AccountOptions.AllowRememberLogin && model.RememberLogin)
                {
                    props = new AuthenticationProperties
                    {
                        IsPersistent = true,
                        ExpiresUtc   = DateTimeOffset.UtcNow.Add(AccountOptions.RememberMeLoginDuration)
                    };
                }
                ;

                // issue authentication cookie for user
                await _events.RaiseAsync(new UserLoginSuccessEvent(user.Username, user.SubjectId, user.Username));

                await HttpContext.SignInAsync(user.SubjectId, user.Username, props);

                // delete temporary cookie used for 2FA
                await HttpContext.SignOutAsync(IdentityServerConstants.DefaultCookieAuthenticationScheme + ".2FA");

                if (_interaction.IsValidReturnUrl(model.ReturnUrl) || Url.IsLocalUrl(model.ReturnUrl))
                {
                    return(Redirect(model.ReturnUrl));
                }

                return(Redirect("~/"));
            }

            // something went wrong, show an error
            return(View(model));
        }
Example #4
0
        protected async Task <IUserDto> GetUserBySubjectAsync(IPrincipal subject)
        {
            var  subjectId = subject.GetSubjectId();
            Guid userKey;

            if (subjectId == null || !Guid.TryParse(subjectId, out userKey))
            {
                return(null);
            }

            var user = await Task.Run(() => BackingUserService.GetUser(userKey, null));

            return(user);
        }
Example #5
0
        private async Task <T> GetUser(IPrincipal principal)
        {
            var userId = principal.GetSubjectId();

            return(await UserManager.FindByIdAsync(userId));
        }