示例#1
0
        /// <summary>
        /// Logs a user in
        /// </summary>
        /// <param name="argRole">The user's role</param>
        /// <param name="argUsername">The username</param>
        /// <param name="argPassword">The password</param>
        /// <returns>The user's JWT</returns>
        public async Task <UserDTO> LoginAsync(string argRole, string argUsername, SecureString argPassword)
        {
            try
            {
                HttpResponseMessage response = await _carService.Client
                                               .GetAsync(_carService.Client.BaseAddress +
                                                         $"users/{argRole}/login/{argUsername}/{PasswordHelpers.ConvertToUnsecureString(argPassword)}");

                if (!response.IsSuccessStatusCode)
                {
                    switch (response.StatusCode)
                    {
                    case HttpStatusCode.Unauthorized:
                        throw new Exception("Invalid username or password!");

                    default:
                        throw new Exception(await response.Content.ReadAsAsync <string>());
                    }
                }

                return(await response.Content.ReadAsAsync <UserDTO>());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }