Test JWT (edit)
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
public class RegisterViewModel
{
public string UserName { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
}
public class RegisterBindingModel
{
public string Email { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
internal class Program
{
private static string baseUri = "http://localhost:55751";
private static async Task RegisterUserAsync()
{
string tokenEndPoint = baseUri + "/api/Account/Register";
var registerViewModel = new RegisterBindingModel
{
Email = "manhng@outlook.com",
Password = "123456a@A",
ConfirmPassword = "123456a@A"
};
var jsonString = JsonConvert.SerializeObject(registerViewModel);
HttpContent content = new StringContent(jsonString, Encoding.UTF8, "application/json");
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(baseUri);
var message = await httpClient.PostAsync(tokenEndPoint, content);
var responseString = await message.Content.ReadAsStringAsync();
if (!string.IsNullOrWhiteSpace(responseString))
{
Console.WriteLine("Error.");
return;
}
}
Console.WriteLine("Success.");
}
private static async Task CreateNewUserAsync()
{
string tokenEndPoint = baseUri + "/api/Account/Register";
var registerViewModel = new RegisterViewModel
{
UserName = "manhng@outlook.com",
Password = "123456a@",
FirstName = "Manh",
LastName = "Nguyen",
BirthDate = Convert.ToDateTime("2019-05-21")
};
var jsonString = JsonConvert.SerializeObject(registerViewModel);
HttpContent content = new StringContent(jsonString, Encoding.UTF8, "application/json");
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(baseUri);
var message = await httpClient.PostAsync(tokenEndPoint, content);
var responseString = await message.Content.ReadAsStringAsync();
if (!string.IsNullOrWhiteSpace(responseString))
{
Console.WriteLine("Error.");
return;
}
}
}
private static string GetToken()
{
Task<string> task = Task.Run<string>(async () => await GetTokenAsync());
return task.Result;
}
private static async Task<string> GetTokenAsync()
{
string oauthToken = string.Empty;
string tokenEndPoint = baseUri + "/Token";
HttpContent content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{"grant_type", "password"},
{"username", "manhng@outlook.com"},
{"password", "123456a@A"}
});
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(baseUri);
var message = await httpClient.PostAsync(tokenEndPoint, content);
var responseString = await message.Content.ReadAsStringAsync();
if (responseString.Contains("access_token"))
{
var obj = JObject.Parse(responseString);
oauthToken = (string)obj["access_token"];
//var tokenType = (string)obj["token_type"];
//var expiresIn = (int)obj["expires_in"]; //The number of seconds left before the token becomes invalid.
}
}
return oauthToken;
}
private static void Main(string[] args)
{
string token = GetToken();
Console.WriteLine(token);
}
//private static async Task Main(string[] args)
//{
// await RegisterUserAsync();
//}
}
}