Example #1
0
        /// <summary>
        /// Création d'un nouvel utilisateur.
        /// </summary>
        /// <param name="utilisateur">L'objet utilisateur.</param>
        /// <returns>Retourne un objet Utilisateur.</returns>
        public async Task <User> Create(ShortUser user)
        {
            try
            {
                if (_wargameContext.User.SingleOrDefault(x => x.Email == user.Email) != null)
                {
                    throw new Exception("Email already exist. Try with another or login with your nickname.");
                }
                if (_wargameContext.User.SingleOrDefault(x => x.Nickname == user.Nickname) != null)
                {
                    throw new Exception("Nickname already exist. Choose another one or login with your account.");
                }
                // Create user.
                User newUser = new User()
                {
                    Email    = user.Email,
                    Nickname = user.Nickname,
                    Password = GenericService.EncryptText(user.Password, "SHA1"),
                    Points   = 0,
                };

                // Create user.
                await _wargameContext.AddAsync(newUser);

                await _wargameContext.SaveChangesAsync();

                return(newUser);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
Example #2
0
        public async Task <ShortUser> Authenticate(string nickname, string password)
        {
            var user = await Task.Run(() => _wargameContext.User.SingleOrDefault(x => x.Nickname == nickname && x.Password == GenericService.EncryptText(password, "SHA1")));

            // return null if user not found
            if (user == null)
            {
                return(null);
            }

            ShortUser returnedUser = new ShortUser()
            {
                Id       = user.Id,
                Nickname = user.Nickname,
                Email    = user.Email,
            };

            returnedUser.Token = GenerateJSONWebToken(user);

            // authentication successful so return user details without password
            return(returnedUser.WithoutPassword());
        }