/// <summary>
        /// 创建refreshToken
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public override void Create(AuthenticationTokenCreateContext context)
        {
            if (string.IsNullOrEmpty(context.Ticket.Identity.Name))
            {
                return;
            }

            var refreshTokenLifeTime = ConfigHelper.GetValue("TokenExpireMinute", "120");

            if (string.IsNullOrEmpty(refreshTokenLifeTime))
            {
                return;
            }

            // generate access token
            var refreshTokenId = new RegularGuidGenerator().Create();

            context.Ticket.Properties.IssuedUtc  = DateTime.UtcNow;
            context.Ticket.Properties.ExpiresUtc = DateTime.UtcNow.AddMinutes(double.Parse(refreshTokenLifeTime));

            var cmd = new SaveTokenCommand()
            {
                RefreshTokenId  = refreshTokenId,
                UserId          = new Guid(context.Ticket.Identity.Name),
                IssuedUtc       = DateTime.Parse(context.Ticket.Properties.IssuedUtc.ToString()),
                ExpiresUtc      = DateTime.Parse(context.Ticket.Properties.ExpiresUtc.ToString()),
                ProtectedTicket = context.SerializeTicket()
            };

            // Token没有过期的情况强行刷新,删除老的Token保存新的Token
            _commandBus.Send(cmd);
            context.SetToken(refreshTokenId.ToString());
        }
Beispiel #2
0
        public User(string userName, string password, string phone, string email)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentNullException("登录账号不能为空!");
            }

            if (string.IsNullOrWhiteSpace(email))
            {
                throw new ArgumentNullException("邮箱不能为空!");
            }

            if (string.IsNullOrWhiteSpace(phone))
            {
                throw new ArgumentNullException("手机号不能为空!");
            }

            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentNullException("密码不能为空!");
            }

            if (password.Trim().Length < 6)
            {
                throw new ArgumentNullException("密码长度应超过6个字符!");
            }

            if (!RegexHelper.IsMobilePhone(phone))
            {
                throw new ArgumentNullException("请输入正确的手机号!");
            }

            if (!RegexHelper.IsEmail(email))
            {
                throw new ArgumentNullException("请输入正确的邮箱!");
            }

            Id       = new RegularGuidGenerator().Create();
            UserName = userName;
            Email    = email;
            Phone    = phone;
            Password = password;
        }