Ejemplo n.º 1
0
        public AspNetUserVM Authenticate(AspNetUserVM vm)
        {
            var user = users.SingleOrDefault(x => x.UserName == vm.UserName && x.Password == vm.Password);

            if (user == null)
            {
                return(null);
            }

            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(appSettings.Key);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, user.UserName),
                    new Claim(ClaimTypes.Role, "Admin"),
                    new Claim(ClaimTypes.Version, "V3.1")
                }),
                Expires            = DateTime.UtcNow.AddDays(2),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescriptor);

            user.Token = tokenHandler.WriteToken(token);

            user.Password = null;
            return(user);
        }
Ejemplo n.º 2
0
        public async Task <RequestResult> Register(AspNetUserVM model)
        {
            if (!ModelState.IsValid)
            {
                return(new RequestResult {
                    Message = "All fields are required", Status = false
                });
            }
            var user = usersBL.GetUserByEmail(model.Email);

            if (user == null)
            {
                AspNetUserDVM usern = new AspNetUserDVM
                {
                    AccessFailedCount = model.AccessFailedCount,
                    ConcurrencyStamp  = model.ConcurrencyStamp,
                    Email             = model.Email,
                    UserName          = model.UserName,
                    Password          = model.Password
                };

                bool result = await usersBL.AddUser(usern, "App User");

                if (result)
                {
                    return(new RequestResult {
                        Message = "Password has been changed.", Status = true
                    });
                }
            }
            return(new RequestResult {
                Message = "Unable to change your password. Please try again.", Status = false
            });
        }
Ejemplo n.º 3
0
        public IActionResult Post([FromBody] AspNetUserVM vm)
        {
            var user = authentication.Authenticate(vm);

            if (user == null)
            {
                return(BadRequest(new { message = "Username and Password is incorrect" }));
            }
            return(Ok(user));
        }
Ejemplo n.º 4
0
 public async Task <JsonResult> UpdateCredentials(AspNetUserVM vm)
 {
     try {
         var aspUser = _userService.GetByIDAsp(vm.ID).Result;
         aspUser.UserName = vm.Username;
         _userService.Update(aspUser);
         _manager.RemovePassword(vm.ID);
         _manager.AddPassword(vm.ID, vm.Password);
         return(Json(new { success = true }));
     } catch { return(Json(new { success = false })); }
 }