public async Task <AuthenticateResultModel> Authenticate([FromBody] AuthenticateModel model)
        {
            try
            {
                var loginResult = await GetLoginResultAsync(
                    model.UserNameOrEmailAddress,
                    model.Password,
                    GetTenancyNameOrNull()
                    );

                var accessToken = CreateAccessToken(CreateJwtClaims(loginResult.Identity));

                return(new AuthenticateResultModel
                {
                    IsLoginSuccess = true,
                    AccessToken = accessToken,
                    EncryptedAccessToken = GetEncrpyedAccessToken(accessToken),
                    ExpireInSeconds = (int)_configuration.Expiration.TotalSeconds,
                    UserId = loginResult.User.Id
                });
            }
            catch (Exception ex)
            {
                var key             = string.Empty;
                var exceptionResult = new AuthenticateResultModel
                {
                    IsLoginSuccess = false,
                    LoginMessage   = ex.Message + "<br/> Username or Password is Incorrect.",
                    Key            = key
                };

                var userExists = await _userManager.FindByNameOrEmailAsync(model.UserNameOrEmailAddress);

                if (userExists == null)
                {
                    return(exceptionResult);
                }

                var appUser = await _applicationUserReposatory.GetAll().FirstOrDefaultAsync(x => x.UserId == userExists.Id && (!x.IsVerified || !x.IsPasswordCreated));

                if (appUser == null)
                {
                    return(exceptionResult);
                }

                key = await _encryptionDecryptionService.EncryptString(userExists.Id.ToString());

                exceptionResult.Key = key;
                return(exceptionResult);
            }
        }
Ejemplo n.º 2
0
        public async Task <RegisterVerificationResponseDto> VerifyUser(RegisterVerificationDto verificationInput)
        {
            var result = new RegisterVerificationResponseDto();

            try
            {
                var user = await _userManager.GetUserByIdAsync(verificationInput.UserId);

                if (user == null)
                {
                    // Don't reveal that the user does not exist
                    result.IsVerificationSuccess = false;
                    result.VerificationMessage   = "Invalid request";
                    return(result);
                }

                var applicationUser = await _applicationUserReposatory.GetAll().FirstOrDefaultAsync(x => x.UserId == user.Id);

                if (applicationUser == null)
                {
                    // Don't reveal that the user does not exist
                    result.IsVerificationSuccess = false;
                    result.VerificationMessage   = "Invalid request";
                    return(result);
                }

                if (!await _userManager.IsEmailConfirmedAsync(user))
                {
                    CheckErrors(await _userManager.ConfirmEmailAsync(user, verificationInput.VerificationEmailToken.Replace(' ', '+')));
                }

                if (!await _userManager.IsPhoneNumberConfirmedAsync(user))
                {
                    CheckErrors(await _userManager.ChangePhoneNumberAsync(user, user.UserName, verificationInput.VerificationCode.Replace(' ', '+')));
                }

                //set verified flag in sunrise user also

                //here is the success

                var code = await _userManager.GeneratePasswordResetTokenAsync(user);

                var encodedUserId = await _encryptionDecryptionService.EncryptString(user.Id.ToString());


                //update application user state as verified state
                applicationUser.IsVerified = true;
                await _applicationUserReposatory.UpdateAsync(applicationUser);

                CurrentUnitOfWork.SaveChanges();

                result.userKey = encodedUserId;
                result.Token   = code;
                result.IsVerificationSuccess = true;
                result.VerificationMessage   = "User successfully verified";
                return(result);
            }
            catch (Exception ex)
            {
                result.VerificationMessage   = ex.Message;
                result.IsVerificationSuccess = false;
                return(result);
            }
        }