forked from MDCPS/DamageAssessment_Backend
Moving password and mode settings to configuration file for dadeschools offline Token service
This commit is contained in:
@ -6,8 +6,10 @@ using DamageAssesment.Api.UsersAccess.Db;
|
||||
using DamageAssesment.Api.UsersAccess.Interfaces;
|
||||
using DamageAssesment.Api.UsersAccess.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace DamageAssesment.Api.UsersAccess.Services
|
||||
{
|
||||
@ -15,9 +17,11 @@ namespace DamageAssesment.Api.UsersAccess.Services
|
||||
{
|
||||
private readonly UsersAccessDbContext usersAccessDbContext;
|
||||
private readonly JwtSettings jwtSettings;
|
||||
public TokenServiceProvider(IOptions<JwtSettings> options, UsersAccessDbContext usersAccessDbContext)
|
||||
private readonly IConfiguration configuration;
|
||||
public TokenServiceProvider(IOptions<JwtSettings> options, UsersAccessDbContext usersAccessDbContext, IConfiguration configuration)
|
||||
{
|
||||
this.usersAccessDbContext = usersAccessDbContext;
|
||||
this.configuration = configuration;
|
||||
this.jwtSettings = options.Value;
|
||||
}
|
||||
public async Task<string> GenerateToken(Models.User user)
|
||||
@ -55,5 +59,27 @@ namespace DamageAssesment.Api.UsersAccess.Services
|
||||
var jwttoken = new JwtSecurityTokenHandler().WriteToken(token);
|
||||
return new TokenResponse() { jwttoken = jwttoken, refreshtoken = await GenerateToken(user) };
|
||||
}
|
||||
|
||||
public async Task<string> ConvertJsonToDadeSchoolsJwt(string json)
|
||||
{
|
||||
var jsonObject = JObject.Parse(json);
|
||||
var claims = new Claim[jsonObject.Count];
|
||||
int i = 0;
|
||||
foreach (var property in jsonObject.Properties())
|
||||
{
|
||||
claims[i++] = new Claim(property.Name, property.Value.ToString());
|
||||
}
|
||||
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration.GetValue<string>("Dadeschools:TokenClientSecret")));
|
||||
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(claims),
|
||||
Expires = DateTime.UtcNow.AddDays(3),
|
||||
SigningCredentials = credentials
|
||||
};
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
return tokenHandler.WriteToken(token);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user