Пример #1
0
        public async Task <ActionResult <UserModel> > Login(UserRegisterModel UserRegisterModel)
        {
            APPUser APPUser = await _context.Users.SingleOrDefaultAsync(u => u.UserName == UserRegisterModel.UserName.ToLower());

            if (APPUser == null)
            {
                return(Unauthorized("Invalid User name"));
            }

            using var hmac = new HMACSHA512(APPUser.PasswordSalt);

            var passwordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(UserRegisterModel.Password));

            for (int i = 0; i < passwordHash.Length; i++)
            {
                if (passwordHash[i] != APPUser.PasswordHash[i])
                {
                    return(Unauthorized("Invalid Username or Password"));
                }
            }

            return(new UserModel
            {
                UserName = APPUser.UserName,
                UserToken = _tokenService.CreateToken(APPUser)
            });
        }
Пример #2
0
        public async Task <ActionResult <UserModel> > Register(UserRegisterModel UserRegisterModel)
        {
            APPUser APPUser;

            if (await CheckIfUserExists(UserRegisterModel.UserName))
            {
                return(BadRequest("User already exists"));
            }

            using var hmac = new HMACSHA512();

            APPUser = new APPUser()
            {
                UserName     = UserRegisterModel.UserName.ToLower(),
                PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(UserRegisterModel.Password)),
                PasswordSalt = hmac.Key
            };
            _context.Users.Add(APPUser);
            await _context.SaveChangesAsync();

            return(new UserModel
            {
                UserName = APPUser.UserName,
                UserToken = _tokenService.CreateToken(APPUser)
            });
        }
Пример #3
0
 private void RaiseUserProfileChangeEvent(APPUser appUser)
 {   //判断用户的信息是否修改了
     if (userContext.Entry(appUser).Property(nameof(appUser.Name)).IsModified ||
         userContext.Entry(appUser).Property(nameof(appUser.Title)).IsModified ||
         userContext.Entry(appUser).Property(nameof(appUser.Company)).IsModified ||
         userContext.Entry(appUser).Property(nameof(appUser.Avatar)).IsModified)
     {
         capPublisher.Publish("userapi.userProfileChangeEvent", new Dtos.UserIdentity
         {
             UserId  = appUser.Id,
             Avatar  = appUser.Avatar,
             Name    = appUser.Name,
             Company = appUser.Company,
             Title   = appUser.Title
         });
     }
 }
Пример #4
0
 private void RaiseUserprofileChangedEvent(APPUser user)
 {
     if (_userContext.Entry(user).Property(nameof(user.Name)).IsModified ||
         _userContext.Entry(user).Property(nameof(user.Title)).IsModified ||
         _userContext.Entry(user).Property(nameof(user.Company)).IsModified ||
         _userContext.Entry(user).Property(nameof(user.Avatar)).IsModified)
     {
         _capPublisher.PublishAsync("finbook.userapi.user_profile_changed", new UserProfileChangedEvent()
         {
             UserId  = user.Id,
             Name    = user.Name,
             Title   = user.Title,
             Company = user.Company,
             Avatar  = user.Avatar
         });
     }
 }
Пример #5
0
        public string CreateToken(APPUser user)
        {
            var claims = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.NameId, user.UserName)
            };
            var creds = new SigningCredentials(_key, SecurityAlgorithms.HmacSha512Signature);

            var tokenDescriptor = new SecurityTokenDescriptor()
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(7),
                SigningCredentials = creds
            };
            var tokenHandler = new JwtSecurityTokenHandler();
            var token        = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(token));
        }
Пример #6
0
        public async Task <IActionResult> CheckOrCreat(string phone)
        {
            //验证手机号码的格式


            var user = await userContext.Users.SingleOrDefaultAsync(x => x.Phone == phone);

            //如果不存在用户,就创建一个用户
            if (user == null)
            {
                user = new APPUser {
                    Phone = phone, CityId = 1, Gender = 1
                };
                APPUser newAppUser          = new APPUser();
                EntityEntry <APPUser> entry = userContext.Users.Add(user);
                userContext.SaveChanges();
                //获取刚刚创建的user
                user = userContext.Users.Where(x => x.Phone == phone).FirstOrDefault();
            }
            return(Ok(new { user.Id, user.Name, user.Company, user.Title, user.Avatar }));
        }
Пример #7
0
        public async Task <IActionResult> CheckOrCreate(string phone)
        {
            var user = _userContext.Users.SingleOrDefault(u => u.Phone == phone);

            if (user == null)
            {
                user = new APPUser {
                    Phone = phone
                };
                _userContext.Users.Add(user);
                await _userContext.SaveChangesAsync();
            }

            return(Ok(new
            {
                user.Id,
                user.Name,
                user.Company,
                user.Title,
                user.Avatar
            }));
        }