Generate() 공개 정적인 메소드

public static Generate ( ) : VerificationToken
리턴 VerificationToken
예제 #1
0
        public VerifyResult ResetVerificationToken(string email, out VerificationToken token, out int id)
        {
            if (!ValidateAndNormalizeEmail(ref email))
            {
                token = null;
                id    = 0;
                return(VerifyResult.UnknownUser);
            }

            VerificationToken localToken = null;
            int localId = 0;
            var result  = this.Connection.InTransaction(true).Do(
                t =>
            {
                var data = t.ReadOne("SELECT Id, EmailVerified, UserBlocked FROM Users WHERE Email=@email", new { email });

                if (data == null)
                {
                    return(VerifyResult.UnknownUser);
                }

                localId = data.Id;

                if (data.UserBlocked)
                {
                    return(VerifyResult.UserBlocked);
                }

                localToken = VerificationToken.Generate();
                t.Update("Users", new { VerificationToken = localToken.Data }, "Id=@id", new { id = localId });
                return(VerifyResult.Success);
            });

            token = localToken;
            id    = localId;
            return(result);
        }
예제 #2
0
        public RegisterResult Register(string email, string password, out VerificationToken token, out int id)
        {
            if (!ValidateAndNormalizeEmail(ref email))
            {
                id    = 0;
                token = null;
                return(RegisterResult.InvalidEmail);
            }

            if (password != null && !this.ValidatePassword(password))
            {
                id    = 0;
                token = null;
                return(RegisterResult.InvalidPassword);
            }

            byte[] salt = null;
            byte[] hash = null;
            if (password != null)
            {
                hash = this.PasswordHasher.Hash(password, ref salt);
            }

            token = VerificationToken.Generate();

            var user = new
            {
                Salt              = salt,
                Password          = hash,
                Email             = email,
                EmailVerified     = false,
                UserBlocked       = false,
                VerificationToken = token.Data
            };

            try
            {
                int userId = 0;
                var result = this.Connection.InTransaction(true).Do(
                    t =>
                {
                    var userInfo = t.ReadOne("SELECT Id, UserBlocked FROM Users WHERE Email=@email", new { email });
                    if (userInfo != null)
                    {
                        userId = userInfo.Id;
                        return(userInfo.UserBlocked
                            ? RegisterResult.UserBlocked
                            : RegisterResult.DuplicateEmail);
                    }

                    t.Save("Users", user, out userId);
                    return(RegisterResult.Success);
                });

                id = userId;
                return(result);
            }
            catch (DbException exception)
            {
                const int MysqlDuplicateEntryServerErrorCode = 1062;
                object    exceptionData = exception.Data["Server Error Code"];
                if (exceptionData is int && (int)exceptionData == MysqlDuplicateEntryServerErrorCode)
                {
                    id    = 0;
                    token = null;
                    return(RegisterResult.DuplicateEmail);
                }

                throw;
            }
        }