Ejemplo n.º 1
0
        public async Task <AccountModel> SignUp(SignUpDto signUpForm)
        {
            return(await base.Execute <AccountModel>(async() =>
            {
                using (UnitOfWork db = new UnitOfWork())
                {
                    IRepo <AccountEntity> accountRepo = db.GetRepo <AccountEntity>();

                    if (await accountRepo.FirstOrDefault(acc => acc.login == signUpForm.Login) != null)
                    {
                        throw new LoginDuplicationException();
                    }

                    PublicKeyModel publicKeyModel = signUpForm.PublicKey.ToModel <PublicKeyModel>();
                    string password = null;

                    try
                    {
                        if (signUpForm.PublicKey == null)
                        {
                            throw new InvalidKeyException();
                        }

                        AsymmetricAlgorithm algorithm = KeyService.GetKeyPair(publicKeyModel);
                        password = EncryptionService.Decrypt(signUpForm.PasswordEncrypted, algorithm);
                    }
                    catch (KeyNotFoundException) { throw new InvalidKeyException(); }
                    catch (FormatException) { throw new PostValidationException("Password format was not match Base64"); }
                    catch (CryptographicException) { throw new WrongEncryptionException("Password"); }

                    if (!PasswordPattern.IsMatch(password))
                    {
                        throw new InvalidPasswordException();
                    }

                    KeyService.DestroyKeyPair(publicKeyModel);

                    AccountEntity accountEntity = new AccountEntity()
                    {
                        login = signUpForm.Login,
                        password = HashingService.GetHashHex(password),
                        first_name = signUpForm.FirstName,
                        last_name = signUpForm.LastName
                    };

                    string authorizedRoleName = DefaultRoles.AUTHORIZED.ToName();
                    accountEntity.Roles.Add(
                        await db.GetRepo <RoleEntity>().FirstOrDefault(
                            role => role.is_default && role.name == authorizedRoleName
                            )
                        );

                    AccountEntity inserted = await accountRepo.Create(accountEntity);
                    await db.Save();

                    return inserted.ToModel <AccountModel>();
                }
            }));
        }
Ejemplo n.º 2
0
        private string getMD5bySM2(string sm2Password)
        {
            string result = string.Empty;

            try
            {
                IAsymmetricEncryptionService IasymmetricEncryptionService = Sm2Service.Instance;
                result = IasymmetricEncryptionService.Decrypt(HttpContext.Current.Session["PrivateKey"].ToString(), sm2Password);
            }
            catch (Exception ex)
            {
                Config.IlogicLogService.Write(new LogicLog()
                {
                    AppName       = Config.AppName,
                    ClassName     = ClassName,
                    NamespaceName = NamespaceName,
                    MethodName    = MethodBase.GetCurrentMethod().Name,
                    Message       = ex.Message,
                    Oper          = Config.Oper
                });
            }
            return(result);
        }