...
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using Hushian.WebApi;
|
||||
using Hushian.Application.Models;
|
||||
using Hushian.Application.Services;
|
||||
using Hushian.WebApi;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -58,7 +60,14 @@ namespace Hushian.Application
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
services.AddScoped(typeof(AuthService));
|
||||
services.AddScoped(typeof(CompanyService));
|
||||
services.AddScoped(typeof(ConversationService));
|
||||
services.AddScoped(typeof(ExperService));
|
||||
services.AddScoped(typeof(GroupService));
|
||||
services.AddScoped(typeof(UserService));
|
||||
services.AddScoped(typeof(VerificationService));
|
||||
services.Configure<JwtSettings>(configuration.GetSection("JwtSettings"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -67,7 +67,7 @@ namespace Hushian.Application.Services
|
||||
Role="Company",
|
||||
img=Company.logo,
|
||||
MobileOrUserName = Company.Mobile,
|
||||
Token = new JwtSecurityTokenHandler().WriteToken(await GenerateToken(Company.Mobile, Company.ID, "Company"))
|
||||
Token = new JwtSecurityTokenHandler().WriteToken(_jwtSettings.GenerateToken(Company.Mobile, Company.ID, "Company"))
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ namespace Hushian.Application.Services
|
||||
CompanyId = exper.CompanyID,
|
||||
MobileOrUserName = exper.UserName,
|
||||
Role="Exper",
|
||||
Token = new JwtSecurityTokenHandler().WriteToken(await GenerateToken(exper.UserName, exper.ID, "Exper"))
|
||||
Token = new JwtSecurityTokenHandler().WriteToken(_jwtSettings.GenerateToken(exper.UserName, exper.ID, "Exper"))
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -126,31 +126,6 @@ namespace Hushian.Application.Services
|
||||
return Response;
|
||||
}
|
||||
|
||||
public async Task<JwtSecurityToken> GenerateToken(string UserName, int userId, string Role)
|
||||
{
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim(JwtRegisteredClaimNames.Sub,UserName),
|
||||
new Claim(ClaimTypes.NameIdentifier, UserName),
|
||||
new Claim(CustomClaimTypes.Uid,userId.ToString()),
|
||||
new Claim(ClaimTypes.Role, Role)
|
||||
};
|
||||
|
||||
|
||||
var symmetricSecurityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.Key));
|
||||
var signingCredentials = new SigningCredentials(symmetricSecurityKey, SecurityAlgorithms.HmacSha256);
|
||||
|
||||
var jwtSecurityToken = new JwtSecurityToken(
|
||||
issuer: _jwtSettings.Issuer,
|
||||
audience: _jwtSettings.Audience,
|
||||
claims: claims,
|
||||
expires: DateTime.UtcNow.AddMinutes(_jwtSettings.DurationInMinutes),
|
||||
signingCredentials: signingCredentials);
|
||||
|
||||
//user.Token = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
|
||||
//var resultupdateuser = await _userManager.UpdateAsync(user);
|
||||
|
||||
return jwtSecurityToken;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,12 @@
|
||||
using System;
|
||||
using Hushian.Application.Constants;
|
||||
using Hushian.Application.Models;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -18,5 +23,30 @@ namespace Hushian.Application.Services
|
||||
{
|
||||
return d.Hour.ToString() + ":" + d.Minute.ToString();
|
||||
}
|
||||
public static JwtSecurityToken GenerateToken(this JwtSettings _jwtSettings,string UserName, int userId, string Role)
|
||||
{
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim(JwtRegisteredClaimNames.Sub,UserName),
|
||||
new Claim(ClaimTypes.NameIdentifier, UserName),
|
||||
new Claim(CustomClaimTypes.Uid,userId.ToString()),
|
||||
new Claim(ClaimTypes.Role, Role)
|
||||
};
|
||||
|
||||
|
||||
var symmetricSecurityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.Key));
|
||||
var signingCredentials = new SigningCredentials(symmetricSecurityKey, SecurityAlgorithms.HmacSha256);
|
||||
var jwtSecurityToken = new JwtSecurityToken(
|
||||
issuer: _jwtSettings.Issuer,
|
||||
audience: _jwtSettings.Audience,
|
||||
claims: claims,
|
||||
expires: DateTime.UtcNow.AddMinutes(_jwtSettings.DurationInMinutes),
|
||||
signingCredentials: signingCredentials);
|
||||
|
||||
//user.Token = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
|
||||
//var resultupdateuser = await _userManager.UpdateAsync(user);
|
||||
|
||||
return jwtSecurityToken;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -12,6 +12,12 @@ namespace Hushian.Application.Services
|
||||
public class UserService
|
||||
{
|
||||
private readonly IGenericRepository<User> _UserRepository;
|
||||
|
||||
public UserService(IGenericRepository<User> userRepository)
|
||||
{
|
||||
_UserRepository = userRepository;
|
||||
}
|
||||
|
||||
public async Task<bool> AnyUser(int UserID) =>await _UserRepository.Get().AnyAsync(a=>a.ID==UserID);
|
||||
|
||||
}
|
||||
|
@@ -11,31 +11,33 @@ using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using Hushian.Application.Models.Message;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Hushian.Application.Services
|
||||
{
|
||||
public class VerificationService
|
||||
{
|
||||
private readonly JwtSettings _jwtSettings;
|
||||
private readonly IGenericRepository<VerificationCode> _VerificationCodeRepository;
|
||||
private readonly IGenericRepository<Company> _CompanyRepository;
|
||||
private readonly IGenericRepository<Exper> _ExperRepository;
|
||||
private readonly IMessageSender _messageSender;
|
||||
private readonly IGenericRepository<User> _UserRepository;
|
||||
private readonly AuthService _authService;
|
||||
|
||||
public VerificationService(IGenericRepository<VerificationCode> verificationCodeRepository
|
||||
, IMessageSender messageSender
|
||||
, IGenericRepository<User> userRepository
|
||||
, AuthService authService
|
||||
, IGenericRepository<Company> companyRepository
|
||||
, IGenericRepository<Exper> experRepository)
|
||||
, IGenericRepository<Exper> experRepository
|
||||
, IOptions<JwtSettings> jwtSettings)
|
||||
{
|
||||
_VerificationCodeRepository = verificationCodeRepository;
|
||||
_messageSender = messageSender;
|
||||
_UserRepository = userRepository;
|
||||
_authService = authService;
|
||||
_CompanyRepository = companyRepository;
|
||||
_ExperRepository = experRepository;
|
||||
_jwtSettings = jwtSettings.Value;
|
||||
}
|
||||
|
||||
public async Task<int> GenerateCodeForLoginUser(string Mobile)
|
||||
@@ -93,7 +95,7 @@ namespace Hushian.Application.Services
|
||||
Id = User.ID,
|
||||
MobileOrUserName = User.Mobile,
|
||||
Role="User",
|
||||
Token = new JwtSecurityTokenHandler().WriteToken(await _authService.GenerateToken(User.Mobile, User.ID, "User"))
|
||||
Token = new JwtSecurityTokenHandler().WriteToken(_jwtSettings.GenerateToken(User.Mobile, User.ID, "User"))
|
||||
};
|
||||
}
|
||||
else
|
||||
@@ -156,7 +158,7 @@ namespace Hushian.Application.Services
|
||||
Role = "Company",
|
||||
img = anyCompany.logo,
|
||||
MobileOrUserName = anyCompany.Mobile,
|
||||
Token = new JwtSecurityTokenHandler().WriteToken(await _authService.GenerateToken(anyCompany.Mobile, anyCompany.ID, "Company"))
|
||||
Token = new JwtSecurityTokenHandler().WriteToken(_jwtSettings.GenerateToken(anyCompany.Mobile, anyCompany.ID, "Company"))
|
||||
};
|
||||
}
|
||||
else
|
||||
|
Reference in New Issue
Block a user