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 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); } }; }