/// <summary> /// Clients the HTTP get list. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <typeparam name="Query">The type of the uery.</typeparam> /// <param name="partialURI">The partial URI.</param> /// <param name="isawait">if set to <c>true</c> [isawait].</param> /// <returns></returns> public async Task <TResult> ClientHTTPGetList <TResult, Query>(string partialURI, bool isawait, bool enableRandomNumber) where TResult : new() { using (var client = new HttpClient()) { this.CreateHttpHeader(client); string entityname = GetAPIControllerName <Query>(); string routingUrl = string.Format("api/{0}/{1}/?", entityname, partialURI); if (enableRandomNumber) { routingUrl += VerifyTransactionSN.GenerateRandomInt(); } HttpResponseMessage response = await client.GetAsync(routingUrl).ConfigureAwait(isawait); log.DebugFormat("请求URL:{0} 结果:{1}", client.BaseAddress + routingUrl, response.IsSuccessStatusCode); var results = new TResult(); results = await ReadAsObject(response, results); return(results); } }
/// <summary> /// Clients the HTTP get string. /// </summary> /// <typeparam name="Query">The type of the uery.</typeparam> /// <param name="paramstr">The paramstr.</param> /// <param name="customURL">The custom URL.</param> /// <param name="isawait">if set to <c>true</c> [isawait].</param> /// <returns></returns> public async Task <string> ClientHTTPGetString <Query>(string querystring, string customURL, bool isawait) where Query : new() { string entityname = typeof(Query).Name; entityname = entityname.Substring(0, entityname.IndexOf("Dto")); string routingUrl = string.Empty; if (string.IsNullOrEmpty(customURL)) { routingUrl = string.Format("api/{0}/{1}", entityname, querystring); } else { routingUrl = string.Format("api/{0}/{1}?{2}", entityname, customURL, querystring); } routingUrl += "&" + VerifyTransactionSN.GenerateRandomInt(); HttpResponseMessage response = await client.GetAsync(routingUrl).ConfigureAwait(isawait); string results = string.Empty; if (response.IsSuccessStatusCode) { results = response.Content.ReadAsStringAsync().Result; } return(results); }
public async Task TestClientDelete() { using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:3956/"); CreateAuthDelegateHeader(client); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); string date = DateTime.UtcNow.ToString("u"); client.DefaultRequestHeaders.Add("Timestamp", date); //string token=CreateToken() // HTTP DELETE string uri = "/api/Values/2"; string methodType = HttpMethod.Delete.ToString().ToUpper(); string querystring = ""; string message = string.Join("\n", methodType, date, uri.ToLower(), querystring); string token = VerifyTransactionSN.ComputeHash(SecurityConfig.Password, message); Console.WriteLine("message:{0} token:{1}", message, token); client.DefaultRequestHeaders.Add("Authentication", string.Format("{0}:{1}", SecurityConfig.Password, token)); var response = await client.DeleteAsync(uri); Assert.True(response.IsSuccessStatusCode); }; }
public async Task TestClientPutWithJsonFormatInRequestBody() { using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:3956/"); CreateAuthDelegateHeader(client); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); string date = DateTime.UtcNow.ToString("u"); client.DefaultRequestHeaders.Add("Timestamp", date); //string token=CreateToken() // HTTP PUT string uri = "/api/Values/UpdateWithPut"; string methodType = HttpMethod.Put.ToString().ToUpper(); var postmodel = new CityModel { CityShortName = "文山", IcaoCode = "ZPPP" }; string querystring = restclient.GetQueryString(postmodel, null); string message = string.Join("\n", methodType, date, uri.ToLower(), querystring); string token = VerifyTransactionSN.ComputeHash(SecurityConfig.Password, message); Console.WriteLine("message:{0} token:{1}", message, token); client.DefaultRequestHeaders.Add("Authentication", string.Format("{0}:{1}", SecurityConfig.Password, token)); string routingUrl = uri + "?" + querystring; var response = await client.PutAsJsonAsync(uri, postmodel); Assert.True(response.IsSuccessStatusCode); }; }
/// <summary> /// Creates the authentication header. /// </summary> /// <param name="client">The client.</param> /// <param name="date">The date.</param> /// <param name="querystring">The querystring.</param> /// <param name="routingUrl">The routing URL.</param> private void CreateAuthenticationHeader(HttpClient client, string date, string querystring, string routingUrl, HttpMethod httpMethod) { string message = string.Join("\n", httpMethod.Method, date, routingUrl.ToLower(), querystring); log.DebugFormat("Client side Message {0}", message); Hashtable remoteDataSource = (Hashtable)WebConfigurationManager.GetSection(this.Section); string password = (string)remoteDataSource["password"]; string token = VerifyTransactionSN.ComputeHash(password, message); log.DebugFormat("Client side token {0}", token); client.DefaultRequestHeaders.Add("Authentication", string.Format("{0}:{1}", password, token)); client.DefaultRequestHeaders.Add("Timestamp", date); }
/// <summary> /// The is authenticated. /// </summary> /// <param name="hashedPassword"> /// The hashed password. /// </param> /// <param name="message"> /// The message. /// </param> /// <param name="signature"> /// The signature. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> private static bool IsAuthenticated(string hashedPassword, string message, string signature) { if (string.IsNullOrEmpty(hashedPassword)) { return(false); } log.DebugFormat("Server Side Message:{0}", message); var verifiedHash = VerifyTransactionSN.ComputeHash(hashedPassword, message); log.DebugFormat("Server Side verifiedHash:{0}", verifiedHash); if (signature != null && signature.Equals(verifiedHash)) { return(true); } return(false); }
public void BCryptTest() { string password = "******"; int workFactor = 13; var start = DateTime.UtcNow; var hashed = VerifyTransactionSN.GenerateVariantHashString(password, workFactor); var end = DateTime.UtcNow; Console.WriteLine("hash length is {0} chars", hashed.Length); Console.WriteLine("Processing time is {0} with workFactor {1}", end - start, workFactor); Console.WriteLine("Hashed password: {0} ", hashed); Console.WriteLine("correct password {0}", VerifyTransactionSN.VerifyVariantHash("PASSWORD", hashed)); Console.WriteLine("incorrect password {0}", VerifyTransactionSN.VerifyVariantHash("PASSWORd", hashed)); Assert.True(VerifyTransactionSN.VerifyVariantHash("PASSWORD", hashed)); }
/// <summary> /// Clients the HTTP get list. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <typeparam name="Query">The type of the uery.</typeparam> /// <param name="partialURI">The partial URI.</param> /// <param name="isawait">if set to <c>true</c> [isawait].</param> /// <returns></returns> public async Task <TResult> ClientHTTPGetList <TResult, Query>(string partialURI, bool isawait, bool enableRandomNumber) where TResult : new() { string entityname = GetAPIControllerName <Query>(); string routingUrl = string.Format("api/{0}/{1}/?", entityname, partialURI); if (enableRandomNumber) { routingUrl += VerifyTransactionSN.GenerateRandomInt(); } HttpResponseMessage response = await client.GetAsync(routingUrl).ConfigureAwait(isawait); var results = new TResult(); results = await ReadAsObject(response, results); return(results); }
public async Task TestWithHttpClient() { using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:3956/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); CreateAuthDelegateHeader(client); string date = DateTime.UtcNow.ToString("u"); // HTTP GET string uri = "/api/Values"; string methodType = "GET"; string querystring = "id=11"; string message = string.Join("\n", methodType, date, uri.ToLower(), querystring); string token = VerifyTransactionSN.ComputeHash(SecurityConfig.Password, message); Console.WriteLine(token); // token = "ppj7m0bW4feYueB770wEb8t+wds/09Gy7ZONlbLTxes="; client.DefaultRequestHeaders.Add("Authentication", string.Format("{0}:{1}", SecurityConfig.Password, token)); client.DefaultRequestHeaders.Add("Timestamp", date); string routingUrl = uri + "?" + querystring; var response = await client.GetAsync(routingUrl); Assert.True(response.IsSuccessStatusCode); if (response.IsSuccessStatusCode) { Console.WriteLine(response.StatusCode); string result = await response.Content.ReadAsStringAsync(); string expectedResult = "\"value\""; Assert.Equal(expectedResult, result); } }; }