first commit
This commit is contained in:
65
Complex.EndPoint/Utility/TokenValidator.cs
Normal file
65
Complex.EndPoint/Utility/TokenValidator.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user