Skip to content

Instantly share code, notes, and snippets.

@hatelove
Last active September 14, 2019 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hatelove/d7a3e95e7b0618496cdee5c603bb4979 to your computer and use it in GitHub Desktop.
Save hatelove/d7a3e95e7b0618496cdee5c603bb4979 to your computer and use it in GitHub Desktop.
use HttpClient Post to web service
public string GetOtp(string accountId)
{
var httpClient = new HttpClient() {BaseAddress = new Uri("http://joey.com/")};
var response = httpClient.PostAsJsonAsync("api/otps", accountId).Result;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsAsync<string>().Result;
}
else
{
throw new Exception($"web api error, accountId:{accountId}");
}
}
@chingmiou
Copy link

        public bool Verify(string accountId, string password, string otp)
        {
            string currentPassword;
            using (var connection = new SqlConnection("datasource=db,password=abc"))
            {
                currentPassword = connection.Query<string>("spGetUserPassword", new {Id = accountId},
                    commandType: CommandType.StoredProcedure).SingleOrDefault();
            }

            var crypt = new System.Security.Cryptography.SHA256Managed();
            var hash = new StringBuilder();
            var crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(password));
            foreach (var theByte in crypto)
            {
                hash.Append(theByte.ToString("x2"));
            }

            var hashPassword = hash.ToString();

            var httpClient = new HttpClient() {BaseAddress = new Uri("http://joey.com/")};
            var response = httpClient.PostAsJsonAsync("api/otps", accountId).Result;
            string currentOtp;
            if (response.IsSuccessStatusCode)
            {
                currentOtp = response.Content.ReadAsAsync<string>().Result;
            }
            else
            {
                throw new Exception($"web api error, accountId:{accountId}");
            }


            if (hashPassword == currentPassword && otp == currentOtp)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment