66 lines
2.3 KiB
C#
66 lines
2.3 KiB
C#
using Complex.Application.PersonService;
|
|
using Complex.Application.Utility;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Linq;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace WebApi.Bugeto.Models.Services.Validator
|
|
{
|
|
public interface ITokenValidator
|
|
{
|
|
Task Execute(TokenValidatedContext context);
|
|
}
|
|
public class TokenValidate : ITokenValidator
|
|
{
|
|
private ISiteAccountlService _siteAccountlService;
|
|
private IUserTokenService _userTokenService;
|
|
public TokenValidate(ISiteAccountlService siteAccountlService, IUserTokenService userTokenService)
|
|
{
|
|
this._siteAccountlService = siteAccountlService;
|
|
this._userTokenService = userTokenService;
|
|
}
|
|
public async Task Execute(TokenValidatedContext context)
|
|
{
|
|
var claimsidentity = context.Principal.Identity as ClaimsIdentity;
|
|
if (claimsidentity?.Claims == null || !claimsidentity.Claims.Any())
|
|
{
|
|
context.Fail("claims not found....");
|
|
return;
|
|
}
|
|
|
|
var userMobileNumber = claimsidentity.FindFirst("UserId").Value.SimpleDeCodeString();
|
|
if (userMobileNumber=="")//!long.TryParse(userId, out long userGuid))
|
|
{
|
|
context.Fail("claims not found....");
|
|
return;
|
|
}
|
|
|
|
var user = _siteAccountlService.GetUser(userMobileNumber);
|
|
|
|
if (user.IsActive == false)
|
|
{
|
|
context.Fail("User not Active");
|
|
return;
|
|
}
|
|
|
|
if (!(context.SecurityToken is JwtSecurityToken Token)
|
|
|| !_userTokenService.CheckExistToken(Token.RawData))
|
|
{
|
|
context.Fail("توکن شناسایی نشد");
|
|
return;
|
|
}
|
|
//context.HttpContext.User.AddIdentity(new System.Security.Claims.ClaimsIdentity(new List<Claim>
|
|
//{
|
|
// new Claim(ClaimTypes.MobilePhone, userMobileNumber),
|
|
//}));
|
|
var claimsIdentity = (ClaimsIdentity)context.Principal.Identity;
|
|
//add your custom claims here
|
|
claimsIdentity.AddClaim(new Claim("MobilePhone", userMobileNumber));
|
|
}
|
|
}
|
|
}
|