private HttpWebRequest BuildWebRequest <T>(T parameter, bool json) where T : BaseCommand { parameter.TerminalKey = _termianlKey; parameter.Token = TokenGeneratorHelper.GenerateToken(parameter, _password); var request = WebRequest.CreateHttp(_baseUrl + parameter.CommandName); request.Method = "POST"; request.ContentType = json ? "application/json" : "x-www-form-urlencoded"; var data = JsonConvert.SerializeObject(parameter, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, }); if (!json) { data = UrlEncode(data); } var postBytes = Encoding.UTF8.GetBytes(data); request.ContentLength = postBytes.Length; using (var stream = request.GetRequestStream()) { stream.Write(postBytes, 0, postBytes.Length); } return(request); }
private HttpRequestMessage BuildRequest <T>(T parameter, bool json) where T : BaseCommand { parameter.TerminalKey = _termianlKey; parameter.Token = TokenGeneratorHelper.GenerateToken(parameter, _password); var request = new HttpRequestMessage(HttpMethod.Post, _baseUrl + parameter.CommandName); var data = JsonConvert.SerializeObject(parameter, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, }); request.Content = json ? (HttpContent) new StringContent(data, Encoding.UTF8, "application/json") : (HttpContent) new FormUrlEncodedContent(JsonConvert.DeserializeObject <Dictionary <string, string> >(data) !); return(request); }
private async Task <E> SendRequestPost <T, E>(T parameter, CancellationToken token) where T : BaseCommand where E : class { parameter.TerminalKey = _termianlKey; parameter.Token = TokenGeneratorHelper.GenerateToken(parameter, _password); var json = JsonConvert.SerializeObject(parameter, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, }); var response = await SendRequestPost(parameter.CommandName, json, token); if (string.IsNullOrEmpty(response)) { return(null); } return(JsonConvert.DeserializeObject <E>(response)); }
public async Task <TokenDto> LoginAsync(LoginAccountDto accountDto, CancellationToken ct = default) { Logger.LogInformation("Login to account {@Account}", accountDto); AccountBase accountBase = await UnitOfWork.AccountRepository.GetUserAsync(accountDto.Email, ct); if (accountBase == null) { Logger.LogInformation("Account with email {Email} doesn't exist", accountDto.Email); throw new AccountNotFoundException(); } if (!accountBase.IsPasswordEqual(accountDto.Password)) { Logger.LogInformation("Password for email {Email} is wrong", accountDto.Email); throw new WrongPasswordException(); } string token = TokenGeneratorHelper.GenerateToken(_configuration, GenerateClaims(accountBase)); return(new TokenDto(token, _configuration.ExpiresInDay)); }
public bool CheckToken(string password) { var generatedToken = TokenGeneratorHelper.GenerateToken(this, password); return(string.Equals(generatedToken, Token)); }