Hi
Is there any link or tutorial available to do below task using - freeapi.gerasim.in/index.html
You first need to use below API to register user
POST? : /api/JWT/CreateNewUser
than using your email and password you can use below api to generate token
POST?: /api/JWT/login
Thanks
Arnab MukherjeePosted Apr 23, 2024, 5:18 PM
Hi Ramco, you can use the below code for your reference.
using Newtonsoft.Json;
using System.Text;
namespace UserRegistration
{
public class UserRequest
{
public int UserId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string MobileNo { get; set; }
public string EmailId { get; set; }
public string AltMobileNo { get; set; }
public string Password { get; set; }
public UserAddress UserAddress { get; set; }
public UserSocialDetails UserSocialDetails { get; set; }
}
public class UserAddress
{
public string City { get; set; }
public string State { get; set; }
public string Pincode { get; set; }
public string AddressLine { get; set; }
}
public class UserSocialDetails
{
public string FacebookProfileUrl { get; set; }
public string LinkdinProfileUrl { get; set; }
public string InstagramHandle { get; set; }
public string TwitterHandle { get; set; }
}
public class TokenRequest
{
public string EmailId { get; set; }
public string Password { get; set; }
}
public class TokenResponse
{
public string Token { get; set; }
}
class Program
{
private static readonly HttpClient client = new HttpClient();
static async Task Main(string[] args)
{
string apiUrl = "https://freeapi.gerasim.in/api/JWT/CreateNewUser";
var userRequest = new UserRequest
{
UserId = 0,
FirstName = "string",
MiddleName = "string",
LastName = "string",
MobileNo = "string",
EmailId = "string",
AltMobileNo = "string",
Password = "string",
UserAddress = new UserAddress
{
City = "string",
State = "string",
Pincode = "string",
AddressLine = "string"
},
UserSocialDetails = new UserSocialDetails
{
FacebookProfileUrl = "string",
LinkdinProfileUrl = "string",
InstagramHandle = "string",
TwitterHandle = "string"
}
};
string jsonResponse = await CreateUserAsync(apiUrl, userRequest);
Console.WriteLine(jsonResponse);
await Login();
}
public static async Task Login()
{
string apiUrl = "https://freeapi.gerasim.in/api/JWT/login";
string email = "string";
string password = "string";
TokenRequest requestModel = new TokenRequest
{
EmailId = email,
Password = password
};
string jsonRequest = JsonConvert.SerializeObject(requestModel);
var httpRequest = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(apiUrl),
Content = new StringContent(jsonRequest, Encoding.UTF8, "application/json")
};
HttpResponseMessage response = await client.SendAsync(httpRequest);
if (response.IsSuccessStatusCode)(jsonResponse);
{
string jsonResponse = await response.Content.ReadAsStringAsync();
TokenResponse tokenResponse = JsonConvert.DeserializeObject
Console.WriteLine("Token: " + tokenResponse.Token);
}
else
{
Console.WriteLine("Failed to generate token. Status code: " + response.StatusCode);
}
}
public static async Task
{
string jsonRequest = JsonConvert.SerializeObject(userRequest);
var httpRequest = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(apiUrl),
Content = new StringContent(jsonRequest, Encoding.UTF8, "application/json-patch+json")
};
HttpResponseMessage response = await client.SendAsync(httpRequest);
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
return jsonResponse;
}
else
{
Console.WriteLine("Failed to create user. Status code: " + response.StatusCode);
return null;
}
}
public static async Task GetTokenAsync(string email, string password)
{
string apiUrl = "https://freeapi.gerasim.in/api/JWT/login";
var requestModel = new TokenRequest
{
EmailId = email,
Password = password
};
string jsonRequest = JsonConvert.SerializeObject(requestModel);
var httpRequest = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(apiUrl),
Content = new StringContent(jsonRequest, Encoding.UTF8, "application/json")
};
HttpResponseMessage response = await client.SendAsync(httpRequest);
if (response.IsSuccessStatusCode)(jsonResponse);
{
string jsonResponse = await response.Content.ReadAsStringAsync();
TokenResponse tokenResponse = JsonConvert.DeserializeObject
return tokenResponse.Token;
}
else
{
Console.WriteLine("Failed to generate token. Status code: " + response.StatusCode);
return null;
}
}
}
}
Jobin SPosted Apr 23, 2024, 1:21 PM
Hi Ramco,
https://www.c-sharpcorner.com/article/jwt-token-authentication-using-the-net-core-6-web-api/