...
This commit is contained in:
87
Hushian.Application/Services/AuthService.cs
Normal file
87
Hushian.Application/Services/AuthService.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using Common.Models.Auth;
|
||||
using Common.Models.Auth.CompanySide;
|
||||
using Common.Models.Auth.UserSide;
|
||||
using Hushian.Application.Constants;
|
||||
using Hushian.Application.Contracts.Persistence;
|
||||
using Hushian.Application.Models;
|
||||
using Hushian.Domain.Entites;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hushian.Application.Services
|
||||
{
|
||||
public class AuthService
|
||||
{
|
||||
private readonly JwtSettings _jwtSettings;
|
||||
private readonly IGenericRepository<Company> _CompanyRepository;
|
||||
private readonly IGenericRepository<User> _UserRepository;
|
||||
private readonly IGenericRepository<Exper> _ExperRepository;
|
||||
private readonly VerificationService _verificationService;
|
||||
public AuthService(IOptions<JwtSettings> jwtSettings)
|
||||
{
|
||||
_jwtSettings = jwtSettings.Value;
|
||||
}
|
||||
public async Task<ResponseBase<AuthResponse>> AuthenticationFromCompanySide
|
||||
(AuthRequestFromCompanySide auth)
|
||||
{
|
||||
ResponseBase<AuthResponse> Response = new();
|
||||
return Response;
|
||||
}
|
||||
public async Task<ResponseBase<int>> AuthenticationFromUserSide
|
||||
(AuthRequestFromUserSide auth)
|
||||
{
|
||||
ResponseBase<int> Response = new();
|
||||
if (!await _UserRepository.Get().AnyAsync(a => a.Mobile == auth.Mobile))
|
||||
{
|
||||
if (!await _UserRepository.ADDBool(new User() { Mobile = auth.Mobile, FullName = auth.FullName }))
|
||||
{
|
||||
Response.Errors.Add("خطا در کاربری");
|
||||
}
|
||||
}
|
||||
|
||||
if (Response.Errors.Count==0)
|
||||
{
|
||||
|
||||
Response.Value = await _verificationService.GenerateCodeForLoginUser(auth.Mobile);
|
||||
Response.Success = true;
|
||||
}
|
||||
return Response;
|
||||
}
|
||||
|
||||
public async Task<JwtSecurityToken> GenerateToken(string UserName, int userId)
|
||||
{
|
||||
|
||||
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim(JwtRegisteredClaimNames.Sub,UserName),
|
||||
new Claim(CustomClaimTypes.Uid,userId.ToString())
|
||||
};
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user