first commit
This commit is contained in:
12
Complex.EndPoint/Utility/ActionAttributes.cs
Normal file
12
Complex.EndPoint/Utility/ActionAttributes.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace Complex.EndPoint.Utility
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class CheckAccess: ActionFilterAttribute
|
||||
{
|
||||
public string AccessTitle { set; get; } = "";
|
||||
public bool AvailabilityByAccess { set; get; } = true;
|
||||
public bool Disabled { set; get; } = false;
|
||||
}
|
||||
}
|
||||
126
Complex.EndPoint/Utility/BaseController.cs
Normal file
126
Complex.EndPoint/Utility/BaseController.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
//using Microsoft.AspNetCore.Mvc;
|
||||
//using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
//namespace Complex.EndPoint.Utility
|
||||
//{
|
||||
// public class BaseController : Controller
|
||||
// {
|
||||
// public IPortalService _portalService;
|
||||
// private ILogger? Logger;
|
||||
// public BaseController(IPortalService portalService, ILogger? logger = null)
|
||||
// {
|
||||
// _portalService = portalService;
|
||||
// if (logger != null)
|
||||
// Logger = logger;
|
||||
// }
|
||||
// public long PersonId;
|
||||
|
||||
|
||||
// private PersonInfo _PersonInfo { set; get; }
|
||||
// public PersonInfo PersonInfo
|
||||
// {
|
||||
// get { return _PersonInfo; }
|
||||
// }
|
||||
// private bool? _IsAccessAction = null;
|
||||
// public bool? IsAccessAction
|
||||
// {
|
||||
// get
|
||||
// {
|
||||
// return _IsAccessAction;
|
||||
// }
|
||||
// }
|
||||
// //public string temp;
|
||||
// public override void OnActionExecuting(ActionExecutingContext context)
|
||||
// {
|
||||
// //FileIO.Writeln("controller.txt","1");
|
||||
// try
|
||||
// {
|
||||
// var attributes = context.ActionDescriptor.EndpointMetadata.OfType<CheckAccess>();
|
||||
// string actionFullName = context.ActionDescriptor.DisplayName.Replace("Controller", "");
|
||||
// string ActionNameExecuting = string.Join(".", actionFullName.Substring(0, actionFullName.IndexOf('(')).Split('.').Reverse().Take(2).Reverse()).Trim();
|
||||
// //FileIO.Writeln("controller.txt", actionFullName);
|
||||
|
||||
// if (attributes.Count() > 0 && attributes.First().Disabled)
|
||||
// {
|
||||
// base.OnActionExecuting(context);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// if (ActionNameExecuting == "Portal.Login")// || ActionNameExecuting == "PortalController.ViewPersonInfo")
|
||||
// return;
|
||||
|
||||
// var cookie = context.HttpContext.Request.Cookies.TryGetValue("PortalTC", out string key);
|
||||
// /**/
|
||||
// //key = "110110110-110110110-0987654321qwerttyggffds".SimpleCodeString();
|
||||
// /**/
|
||||
|
||||
// if (!string.IsNullOrEmpty(key))
|
||||
// key = key.SimpleDeCodeString();
|
||||
// else
|
||||
// {
|
||||
// // FileIO.Write("ali.txt", "Key:isEmpty");
|
||||
// context.HttpContext.Response.StatusCode = 401;
|
||||
// context.Result = Json(new ResultDto
|
||||
// {
|
||||
// IsSuccess = false,
|
||||
// Message = ActionNameExecuting //"دسترسی غیر مجاز"
|
||||
// });
|
||||
// return;
|
||||
// }
|
||||
// //FileIO.Write("ali.txt", "Key:" + key);
|
||||
// {
|
||||
// _PersonInfo = _portalService.Login(key);
|
||||
// if (_PersonInfo != null)
|
||||
// PersonId = _PersonInfo.PersonId;
|
||||
// else
|
||||
// {
|
||||
// context.HttpContext.Response.StatusCode = 401;
|
||||
// context.Result = Json(new ResultDto
|
||||
// {
|
||||
// IsSuccess = false,
|
||||
// Message = "ورود ناموفق" //"دسترسی غیر مجاز"
|
||||
// });
|
||||
|
||||
// }
|
||||
// }
|
||||
// if (attributes.Count() > 0)
|
||||
// {
|
||||
// var at = attributes.First();
|
||||
// if (at.AccessTitle.IsNullOrEmpty())
|
||||
// at.AccessTitle = ActionNameExecuting;
|
||||
// _IsAccessAction = _portalService.IsAccess(PersonId, at.AccessTitle);
|
||||
// if (at.AvailabilityByAccess && _IsAccessAction == false)
|
||||
// {
|
||||
// context.HttpContext.Response.StatusCode = 403;
|
||||
// context.Result = Json(new ResultDto
|
||||
// {
|
||||
// IsSuccess = false,
|
||||
// Message = "[" + ActionNameExecuting + "]دسترسی غیر مجاز"
|
||||
// });//NoContent();
|
||||
// }
|
||||
// //temp = PersonId + ":" + ActionNameExecuting + ":" + _IsAccessAction;
|
||||
// }
|
||||
// //FileIO.Writeln("controller.txt","3");
|
||||
// base.OnActionExecuting(context);
|
||||
// //FileIO.Writeln("controller.txt","4");
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// context.HttpContext.Response.StatusCode = 504;
|
||||
// context.Result = Json(new ResultDto
|
||||
// {
|
||||
// IsSuccess = false,
|
||||
// Message = ex.Message
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// internal List<GridCommand> GetAccessCommand(List<GridCommand> commands)
|
||||
// {
|
||||
// var acCommands = _portalService.GetAccessOfList(4, commands.Where(c => c.AccessTitle.IsNotNullOrEmpty())
|
||||
// .Select(c => c.AccessTitle).ToList());
|
||||
|
||||
|
||||
// return commands.Where(c => acCommands.Contains(c.AccessTitle) || c.AccessTitle.IsNullOrEmpty()).ToList();
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
12
Complex.EndPoint/Utility/Extensions.cs
Normal file
12
Complex.EndPoint/Utility/Extensions.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Complex.EndPoint.Utility
|
||||
{
|
||||
public static class Extensions
|
||||
{
|
||||
public static string GetUserId(this ClaimsPrincipal User)
|
||||
{
|
||||
return User.Claims.First(c => c.Type == "MobilePhone").Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
37
Complex.EndPoint/Utility/GridUtitlity.cs
Normal file
37
Complex.EndPoint/Utility/GridUtitlity.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System.Dynamic;
|
||||
|
||||
namespace Complex.EndPoint.Utility
|
||||
{
|
||||
public class GridDataRequest
|
||||
{
|
||||
public int start { set; get; }
|
||||
public int length { set; get; }
|
||||
public string extra_search { set; get; }
|
||||
|
||||
public int Page
|
||||
{
|
||||
get
|
||||
{
|
||||
return start / length;
|
||||
}
|
||||
}
|
||||
public dynamic SeacrchColumns
|
||||
{
|
||||
get
|
||||
{
|
||||
dynamic myobject = new ExpandoObject();
|
||||
|
||||
IDictionary<string, object> dic = myobject;
|
||||
|
||||
var searchCols = this.extra_search.Substring(1, this.extra_search.Length - 2).Replace("\u0022", "").Split(',');
|
||||
foreach (var item in searchCols)
|
||||
{
|
||||
var t = item.Split(':');
|
||||
dic.Add(t[0], t[1]);
|
||||
}
|
||||
return dic;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
19
Complex.EndPoint/Utility/MappingProfile.cs
Normal file
19
Complex.EndPoint/Utility/MappingProfile.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using AutoMapper;
|
||||
using Complex.Application.Basic;
|
||||
using Complex.Application.Complex;
|
||||
using Complex.Domain.Entities;
|
||||
using Complex.EndPoint.ViewModel;
|
||||
|
||||
namespace Complex.Mapping;
|
||||
|
||||
public partial class EndPointMappingProfile : Profile
|
||||
{
|
||||
public EndPointMappingProfile()
|
||||
{
|
||||
//CreateMap<ComplexEntity, ComplexDto>().ReverseMap();
|
||||
//CreateMap<Unit, UnitDto>().ReverseMap();
|
||||
//CreateMap<UnitStateDto, UnitState>().ReverseMap();
|
||||
CreateMap<ComplexViewModel, ComplexDto>().ReverseMap();
|
||||
CreateMap<UnitViewModel, UnitDto>().ReverseMap();
|
||||
}
|
||||
}
|
||||
33
Complex.EndPoint/Utility/SwaggerOperationFilter.cs
Normal file
33
Complex.EndPoint/Utility/SwaggerOperationFilter.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
public class SwaggerOperationFilter : IOperationFilter
|
||||
{
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
var authAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
|
||||
.Union(context.MethodInfo.GetCustomAttributes(true))
|
||||
.OfType<AuthorizeAttribute>();
|
||||
|
||||
if (authAttributes.Any())
|
||||
{
|
||||
var securityRequirement = new OpenApiSecurityRequirement()
|
||||
{
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = "Bearer"
|
||||
}
|
||||
},
|
||||
Array.Empty<string>()
|
||||
}
|
||||
};
|
||||
operation.Security = new List<OpenApiSecurityRequirement> { securityRequirement };
|
||||
operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" });
|
||||
}
|
||||
}
|
||||
}
|
||||
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