Пример #1
0
        // Returns the Client data for a given ClientId.
        // Assumption: each Client has a unique ClientId.
        public async Task <Client> GetClientById(string id)
        {
            try
            {
                IEnumerable <Client> clients = await _clientData.GetAllClients();

                Client client = clients.FirstOrDefault(x => x.Id == id);

                return(client);
            }
            catch (Exception ex)
            {
                _logger.LogError("An error occurred in method GetClientById. {0}", ex);
                throw;
            }
        }
Пример #2
0
        // User data is taken from web service. For simplicity, passwords will be taken from the field Id (guid).
        // For security reasons store this data in a database with hashed passwords in production environment.
        public async Task <User> Authenticate(string userName, string password)
        {
            try
            {
                IEnumerable <Client> users = await _clientData.GetAllClients();

                Client client = await Task.Run(() => users.SingleOrDefault(x => x.Name == userName && x.Id == password));

                // Return null if user not found.
                if (client == null)
                {
                    return(null);
                }
                else
                {
                    // Map Client to User.
                    User user = new User {
                        Id = client.Id, UserName = client.Name, Password = client.Id, Role = client.Role
                    };

                    // Authentication successful so return user details without password.
                    return(user.WithoutPassword());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("An error occurred in method Authenticate. {0}", ex);
                throw;
            }
        }