Esempio n. 1
0
        public Models.LoginResponse Post(Models.LoginRequest model)
        {
            Models.LoginResponse response = new Models.LoginResponse();
            if (ModelState.IsValid)
            {
                if (model.UserName == "admin" && model.Password == "admin123")
                {
                    response.IsSuccess = true;
                    response.Message   = "Login successful.";
                    response.UserId    = 1491;
                    response.UserName  = model.UserName;
                    response.EmailId   = "*****@*****.**";

                    //generate new token for authentication.
                    response.Token = GenerateJsonWebToken(response.UserId.ToString(), response.UserName, response.EmailId);
                }
                else
                {
                    response.Message = "Invalid username/password.";
                }
            }
            else
            {
                response.Message = "Invalid request.";
            }
            return(response);
        }
Esempio n. 2
0
        public Models.LoginResponse Login(string emailAddress, string password)
        {
            Models.LoginResponse loginResponse = null;

            try
            {
                //SQL Statement
                var sqlString = "SELECT id, security_token FROM users WHERE email_address = @email_address AND password = @password";

                using (var connection = new NpgsqlConnection(connectionString))
                {
                    connection.Open();

                    using (var command = new NpgsqlCommand(sqlString, connection))
                    {
                        command.Parameters.AddWithValue("@email_address", NpgsqlTypes.NpgsqlDbType.Text, emailAddress);
                        command.Parameters.AddWithValue("@password", NpgsqlTypes.NpgsqlDbType.Text, password);
                        command.Prepare();

                        using (var reader = command.ExecuteReader())
                        {
                            if (reader != null && reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    //Create and hydrate a new Object
                                    loginResponse = new Models.LoginResponse();

                                    loginResponse.Id            = Guid.Parse(reader["id"].ToString());
                                    loginResponse.SecurityToken = Guid.Parse(reader["security_token"].ToString());
                                }
                            }
                        }
                    }
                }
                return(loginResponse);
            }
            catch (Exception ex)
            {
                //Log Exception
                _logger.LogError(ex, "error retrieving id and security token");
                return(loginResponse);
            }
        }