Ejemplo n.º 1
0
        public async Task <ActionResult <LoginResultDTOout> > Login(UserLoginDataDTOin data)
        {
            AcUser user = um.Users.FirstOrDefault(x => x.UserName == data.UserNameOrEmail || x.Email == data.UserNameOrEmail);

            if (user == null)
            {
                return(StatusCode(401, new { error = "username or password mismatch!" }));
            }

            await sm.SignInAsync(user, false, data.Password);

            if (sm.IsSignedIn(User))
            {
                return(StatusCode(401, new { error = "username or password mismatch!" }));
            }

            await sm.SignOutAsync();

            string token = await jwtService.CreateJWT(user);

            var result = new LoginResultDTOout {
                authToken = token
            };

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <LoginResultDTOout> > Register(UserDataRegisterDTOin userData)
        {
            var newUser   = mapper.Map <AcUser>(userData);
            var regResult = await um.CreateAsync(newUser, userData.Password);

            var roleAssignResult = await um.AddToRoleAsync(newUser, "User");

            if (regResult.Succeeded && roleAssignResult.Succeeded)
            {
                var loginData = new UserLoginDataDTOin {
                    UserNameOrEmail = userData.UserName, Password = userData.Password
                };
                return(await Login(loginData));
            }

            var reasons = regResult.Errors.Select(e => e.Description).ToArray();

            return(StatusCode(401, new { error = reasons }));
            //  return new LoginResultDTOout { isSuccessfull = false, Errors = reasons };
        }