using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Microsoft.IdentityModel.Protocols.WsFed;
using Microsoft.IdentityModel.Tokens;
public static void Main()
Console.WriteLine("Hello World");
public class OneLoginTokenLogic: IOneLoginTokenLogic
public (string, DateTime) GetToken(UserProfile userProfile)
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_configuration["AppSetting:OneLoginJWTSecretKey"]);
var expires = DateTime.Now.AddHours(1);
var tokenDescriptor = new SecurityTokenDescriptor
Subject = this.GetClaimsIdentity(userProfile),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key),
SecurityAlgorithms.HmacSha256Signature)
var token = tokenHandler.CreateToken(tokenDescriptor);
string strToken = tokenHandler.WriteToken(token);
return (strToken, expires);
private ClaimsIdentity GetClaimsIdentity(UserProfile userProfile)
var claimsIdentity = new ClaimsIdentity();
List<Claim> claims = new();
if (!string.IsNullOrWhiteSpace(userProfile.Name))
claims.Add(new Claim(ClaimTypes.Name, userProfile.Name));
if (!string.IsNullOrWhiteSpace(userProfile.ItsID))
claims.Add(new Claim("ItsID", userProfile.ItsID));
if (!string.IsNullOrWhiteSpace(userProfile.ImageUrl))
claims.Add(new Claim("ImageUrl", userProfile.ImageUrl));
if (!string.IsNullOrWhiteSpace(userProfile.EmpID))
claims.Add(new Claim("EmpID", userProfile.EmpID));
foreach (var right in userProfile.Rights)
claims.Add(new Claim("Rights", Newtonsoft.Json.JsonConvert.SerializeObject(right, new JsonSerializerSettings
NullValueHandling = NullValueHandling.Ignore
claimsIdentity = new ClaimsIdentity(claims, "apiauth_type");
public UserProfile GetUserProfile(string token)
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_configuration["AppSetting:OneLoginJWTSecretKey"]);
var tokenValidationParameters = new TokenValidationParameters
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
SecurityToken securityToken;
ClaimsPrincipal principle;
principle = tokenHandler.ValidateToken(token, tokenValidationParameters, out securityToken);
catch (Microsoft.IdentityModel.Tokens.SecurityTokenExpiredException ex)
throw new UnauthorizedAccessException("Session expired. Please login again.");
catch (System.ArgumentException)
throw new UnauthorizedAccessException("Invalid token");
throw new UnauthorizedAccessException("Could not validate token for unknown reason");
JwtSecurityToken jwtSecurityToken = securityToken as JwtSecurityToken;
if (jwtSecurityToken != null && jwtSecurityToken.Header.Alg.Equals(SecurityAlgorithms.HmacSha256, StringComparison.InvariantCultureIgnoreCase))
UserProfile userProfile = new()
ItsID = principle.FindFirst("ItsID")?.Value,
Name = principle.FindFirst(ClaimTypes.Name)?.Value,
EmpID = principle.FindFirst("EmpID")?.Value,
Rights = principle.FindAll("Rights").Select(r => Newtonsoft.Json.JsonConvert.DeserializeObject<Right>(r.Value)).ToList()
var tokenDetails = this.GetToken(userProfile);
userProfile.Token = tokenDetails.Item1;
userProfile.ExpiryTime = tokenDetails.Item2;
throw new UnauthorizedAccessException();
public async Task<Teacher> GetTeacherDataAsync(string teacherItsId)
_dataAccess.ConnectionStringName = "MySql.CentralData";
string query = "SELECT * FROM TeacherTable WHERE Teacher_Id = @TeacherId";
var parameters = new { TeacherId = teacherItsId };
Teacher teacher = await _dataAccess.GetData<Teacher, dynamic>(query, parameters);
public string Teacher_Id { get; set; }
public string U_Name { get; set; }
public string CATEGORY { get; set; }
public string T_Email { get; set; }
public string Gender { get; set; }
public string Address { get; set; }
public string Mobile { get; set; }
public string BIRTHDT_AD { get; set; }
public string BIRTHDT_H { get; set; }
public string Designation { get; set; }
public string Office_Email { get; set; }
public string Branch_ID { get; set; }
public int? Age { get; set; }
public string FarigYear { get; set; }
public string NameAR { get; set; }
public string ITS_DOB { get; set; }
public string ITS_HijriDOB { get; set; }
public string BankBranch { get; set; }
public string BankName { get; set; }
public string BankIFSC { get; set; }
public string BankACName { get; set; }
public string BankACNo { get; set; }
public bool Deleted { get; set; }
public DateTime TimeStamp { get; set; }
public string ItsID { get; set; }
public string Name { get; set; }
public List<Right> Rights { get; set; }
public string Token { get; set; }
public string EmpID { get; set; }
public DateTime ExpiryTime { get; set; }
public string ImageUrl { get; set; }
public int? ModuleID { get; set; }
public string ModuleName { get; set; }
public string ModuleLink { get; set; }
public int? ParentID { get; set; }
public int? AppID { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string Icon { get; set; }
public int? SortNo { get; set; }
public List<Right> Children { get; set; }
public interface IOneLoginTokenLogic
(string, DateTime) GetToken(UserProfile userProfile);
UserProfile GetUserProfile(string token);
Task<Teacher> GetTeacherDataAsync(string teacherItsId);
public int AppId { get; set; }
public string AppName { get; set; }
public string AppKey { get; set; }
public string AppBaseUrl { get; set; }
public int SortNo { get; set; } = 0;