Ejemplo n.º 1
0
        /// <summary>
        /// Login operation
        /// Create a session for user if auth is Ok.
        /// </summary>
        /// <param name="login"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static AuthenticationResponse Login(string login, string password)
        {
            login    = login.Trim();
            password = password.Trim();
            Users user = MongoManager.FirstOrDefault <Users>(x => x.Login == login);

            if (user == null)
            {
                throw new Exception($"There is no user {login} in the Database.");
            }

            if (user.Password != password)
            {
                throw new Exception("The password is wrong.");
            }

            Sessions session = MongoManager.SingleOrDefault <Sessions>(x => x.UserId == user._id);

            if (session == null)
            {
                session = new Sessions(Crypto.GenerateUniqueKey(), user._id);

                MongoManager.AddItemInCollection(session);
            }

            log.Information($"Added session {session._id}.");

            return(new AuthenticationResponse(user, session));
        }