コード例 #1
0
        public IActionResult Register([FromBody] UserCredentials userCredentials)
        {
            if (userCredentials == null || string.IsNullOrWhiteSpace(userCredentials.Username) || string.IsNullOrWhiteSpace(userCredentials.Password))
            {
                return(new Responses.BadRequestResult("Invalid data,"));
            }

            using (var context = new SkoPaTuManaContext())
            {
                if (UserExists(userCredentials, context))
                {
                    return(new Responses.BadRequestResult("User already registered,"));
                }

                context.Users.Add(new Users {
                    Username = userCredentials.Username, Password = userCredentials.Password
                });
                context.SaveChanges();

                return(new Responses.OkResult("User created,"));
            }
        }
コード例 #2
0
        public IActionResult Login([FromBody] UserCredentials userCredentials)
        {
            if (userCredentials == null || string.IsNullOrWhiteSpace(userCredentials.Username) || string.IsNullOrWhiteSpace(userCredentials.Password))
            {
                return(new Responses.BadRequestResult("Invalid credentials,"));
            }

            using (var context = new SkoPaTuManaContext())
            {
                var user = GetUser(userCredentials, context);
                if (user == null)
                {
                    return(new Responses.BadRequestResult("User does not exist,"));
                }

                if (user.Password == userCredentials.Password)
                {
                    return(new Responses.OkResult("Login successful,"));
                }

                return(new Responses.BadRequestResult("Wrong password,"));
            }
        }
コード例 #3
0
 private static Users GetUser(UserCredentials userCredentials, SkoPaTuManaContext context)
 {
     return(context.Users.FirstOrDefault(u => u.Username == userCredentials.Username));
 }
コード例 #4
0
 private static bool UserExists(UserCredentials userCredentials, SkoPaTuManaContext context)
 {
     return(context.Users.Any(u => u.Username == userCredentials.Username));
 }