区分请求主机测试

Uat_Study
hang 2022-05-24 10:45:31 +08:00
parent 993fd4ddaa
commit 4244342e9a
8 changed files with 156 additions and 58 deletions

View File

@ -28,7 +28,10 @@ namespace IRaCIS.Core.API
_enrichAction = (logEvent, propertyFactory, httpContext) =>
{
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("RequestIP", httpContext.Connection.RemoteIpAddress.ToString()));
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("LocalIP", httpContext.Connection.LocalIpAddress.MapToIPv4().ToString()));
//这样读取没用
//logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("RequestBody", await ReadRequestBody(httpContext.Request)));
//logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("RequestIP", IPHelper.GetIP(httpContext.Request) ));

View File

@ -26,7 +26,7 @@ namespace IRaCIS.Core.API
//控制台 方便调试 问题 我们显示记录日志 时 获取上下文的ip 和用户名 用户类型
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Warning,
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} ] {ClientIp} {TokenUserRealName} {TokenUserType} {Message:lj} {Properties:j}{NewLine} {Exception}")
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} ] {LocalIP} {ClientIp} {TokenUserRealName} {TokenUserType} {Message:lj} {Properties:j}{NewLine} {Exception}")
.WriteTo.File($"{AppContext.BaseDirectory}Serilogs/.log", rollingInterval: RollingInterval.Day,
outputTemplate: "{Timestamp:HH:mm:ss} || {Level} || {SourceContext:l} || {Message} ||{Exception} ||end {NewLine}");
//.WriteTo.MSSqlServer("Data Source=DESKTOP-4TU9A6M;Initial Catalog=CoreFrame;User ID=sa;Password=123456", "logs", autoCreateSqlTable: true, restrictedToMinimumLevel: LogEventLevel.Information)//从左至右四个参数分别是数据库连接字符串、表名、如果表不存在是否创建、最低等级。Serilog会默认创建一些列。

View File

@ -0,0 +1,130 @@
using EasyCaching.Core;
using IRaCIS.Core.Domain.Share;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace IRaCIS.Core.Application.BusinessFilter;
public class LimitUserRequestAuthorization : IAsyncAuthorizationFilter
{
private readonly IEasyCachingProvider _provider;
private readonly IUserInfo _userInfo;
public LimitUserRequestAuthorization(IEasyCachingProvider provider, IUserInfo userInfo)
{
_provider = provider;
_userInfo = userInfo;
}
public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
{
if (context.ActionDescriptor.EndpointMetadata.Any(item => item is IAllowAnonymous))
{
//匿名访问的不处理
}
else
{
//1、用户登陆的时候设置缓存
//2、在这里取缓存 进行比较 看是否有其他人进行了登陆,如果其他人登陆了,就把之前用户挤掉
var cacheUserToken = (await _provider.GetAsync<string>(_userInfo.Id.ToString())).Value;
if (cacheUserToken == null)
{
//设置当前用户最新Token
await _provider.SetAsync(_userInfo.Id.ToString(), _userInfo.UserToken, TimeSpan.FromDays(7));
}
//是同一个人
else if(cacheUserToken == _userInfo.UserToken)
{
}
else
{
context.Result = new StatusCodeResult(401);
}
var cacheHostToken = (await _provider.GetAsync<string>(_userInfo.IP.ToString()+_userInfo.LocalIp.ToString())).Value;
if (cacheHostToken == null)
{
//设置当前主机最新Token
await _provider.SetAsync(_userInfo.IP.ToString() + _userInfo.LocalIp.ToString(), _userInfo.UserToken, TimeSpan.FromDays(7));
}
//是同主机
else if (cacheHostToken == _userInfo.UserToken)
{
}
else
{
context.Result = new StatusCodeResult(401);
}
}
}
}
//public class UserTypeRequirement : IAuthorizationRequirement
//{
//}
//public class UserTypeHandler : AuthorizationHandler<UserTypeRequirement>
//{
// private IUserInfo _userInfo;
// public UserTypeHandler(IUserInfo userInfo)
// {
// _userInfo = userInfo;
// }
// protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserTypeRequirement requirement)
// {
// //if (context.User.Claims.Count() == 0)
// //{
// // return Task.CompletedTask;
// //}
// //string userId = context.User.Claims.First(c => c.Type == "Userid").Value;
// //string qq = context.User.Claims.First(c => c.Type == "QQ").Value;
// //if (_UserService.Validata(userId, qq))
// //{
// // context.Succeed(requirement); //验证通过了
// //}
// ////在这里就可以做验证
// return Task.CompletedTask;
// }
//}

View File

@ -1,46 +0,0 @@
using IRaCIS.Core.Infra.EFCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
namespace IRaCIS.Core.Application.BusinessFilter
{
//public class UserTypeRequirement : IAuthorizationRequirement
//{
//}
//public class UserTypeHandler : AuthorizationHandler<UserTypeRequirement>
//{
// private IUserInfo _userInfo;
// public UserTypeHandler(IUserInfo userInfo)
// {
// _userInfo = userInfo;
// }
// protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserTypeRequirement requirement)
// {
// //if (context.User.Claims.Count() == 0)
// //{
// // return Task.CompletedTask;
// //}
// //string userId = context.User.Claims.First(c => c.Type == "Userid").Value;
// //string qq = context.User.Claims.First(c => c.Type == "QQ").Value;
// //if (_UserService.Validata(userId, qq))
// //{
// // context.Succeed(requirement); //验证通过了
// //}
// ////在这里就可以做验证
// return Task.CompletedTask;
// }
//}
}

View File

@ -1,13 +1,11 @@
using AutoMapper;
using IRaCIS.Application.Interfaces;
using IRaCIS.Core.Application.Contracts.Dicom.DTO;
using IRaCIS.Core.Infra.EFCore;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.Service.Inspection.Interface;
using IRaCIS.Core.Application.Service.Inspection.DTO;
using Newtonsoft.Json;
namespace IRaCIS.Application.Services
{

View File

@ -21,25 +21,25 @@ namespace IRaCIS.Application.Services
public string Get()
{
return String.Empty;
return _userInfo.LocalIp;
}
[HttpPost]
public string Get(testModel testModel)
{
var aaabb = _trialRepository.BatchDeleteNoTrackingAsync(t => t.Id == Guid.Empty).Result;
//var aaabb = _trialRepository.BatchDeleteNoTrackingAsync(t => t.Id == Guid.Empty).Result;
var aaaa = _dicRepository.BatchDeleteNoTrackingAsync(t => t.Id == Guid.Empty).Result;
//var aaaa = _dicRepository.BatchDeleteNoTrackingAsync(t => t.Id == Guid.Empty).Result;
var waitModifyEntity = _dicRepository.FirstOrDefaultAsync(t => t.Id == Guid.Parse("e2b97a6c-35a6-4aa3-7f27-08da13ab33ff")).GetAwaiter().GetResult();
//var waitModifyEntity = _dicRepository.FirstOrDefaultAsync(t => t.Id == Guid.Parse("e2b97a6c-35a6-4aa3-7f27-08da13ab33ff")).GetAwaiter().GetResult();
var tt = _dicRepository.UpdateAsync(waitModifyEntity, t => new Dictionary() { Description = "xxxxx" }, true).Result;
//var tt = _dicRepository.UpdateAsync(waitModifyEntity, t => new Dictionary() { Description = "xxxxx" }, true).Result;
var tt2 = _trialRepository.UpdatePartialFromQueryAsync(Guid.Parse("543d0000-3e10-0016-77e9-08da2827228a"), t => new Trial() { Indication = "WCH测试稽查002" }, true).Result;
//var tt2 = _trialRepository.UpdatePartialFromQueryAsync(Guid.Parse("543d0000-3e10-0016-77e9-08da2827228a"), t => new Trial() { Indication = "WCH测试稽查002" }, true).Result;
@ -68,7 +68,7 @@ namespace IRaCIS.Application.Services
var b = _localizer["test{0}", "测试"];
//return _localizer["test{0}", "测试"];
return _userInfo.RequestUrl;
return _userInfo.LocalIp;
}
}

View File

@ -39,6 +39,8 @@ namespace IRaCIS.Core.Domain.Share
string IP { get; }
string LocalIp { get; }
bool IsEn_Us { get; }
string RequestUrl { get; }

View File

@ -185,6 +185,17 @@ namespace IRaCIS.Core.Domain.Share
}
}
public string LocalIp
{
get
{
return _accessor?.HttpContext?.Request.Host.Value;
}
}
public bool IsEn_Us
{
get