:sparkles:新增单点登录功能、在线时长
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
using Infrastructure.Attribute;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Infrastructure
|
||||
{
|
||||
/// <summary>
|
||||
/// App服务注册
|
||||
/// </summary>
|
||||
public static class AppServiceExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 注册引用程序域中所有有AppService标记的类的服务
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
public static void AddAppService(this IServiceCollection services)
|
||||
{
|
||||
var cls = AppSettings.Get<string[]>("InjectClass");
|
||||
if (cls == null || cls.Length <= 0)
|
||||
{
|
||||
throw new Exception("请更新appsettings类");
|
||||
}
|
||||
foreach (var item in cls)
|
||||
{
|
||||
Register(services, item);
|
||||
}
|
||||
}
|
||||
|
||||
private static void Register(IServiceCollection services, string item)
|
||||
{
|
||||
Assembly assembly = Assembly.Load(item);
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
var serviceAttribute = type.GetCustomAttribute<AppServiceAttribute>();
|
||||
|
||||
if (serviceAttribute != null)
|
||||
{
|
||||
var serviceType = serviceAttribute.ServiceType;
|
||||
//情况1 适用于依赖抽象编程,注意这里只获取第一个
|
||||
if (serviceType == null && serviceAttribute.InterfaceServiceType)
|
||||
{
|
||||
serviceType = type.GetInterfaces().FirstOrDefault();
|
||||
}
|
||||
//情况2 不常见特殊情况下才会指定ServiceType,写起来麻烦
|
||||
if (serviceType == null)
|
||||
{
|
||||
serviceType = type;
|
||||
}
|
||||
|
||||
switch (serviceAttribute.ServiceLifetime)
|
||||
{
|
||||
case LifeTime.Singleton:
|
||||
services.AddSingleton(serviceType, type);
|
||||
break;
|
||||
case LifeTime.Scoped:
|
||||
services.AddScoped(serviceType, type);
|
||||
break;
|
||||
case LifeTime.Transient:
|
||||
services.AddTransient(serviceType, type);
|
||||
break;
|
||||
default:
|
||||
services.AddTransient(serviceType, type);
|
||||
break;
|
||||
}
|
||||
//System.Console.WriteLine($"注册:{serviceType}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
using Infrastructure.Extensions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using UAParser;
|
||||
|
||||
namespace Infrastructure.WebExtensins
|
||||
{
|
||||
/// <summary>
|
||||
/// HttpContext扩展类
|
||||
/// </summary>
|
||||
public static partial class HttpContextExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否是ajax请求
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsAjaxRequest(this HttpRequest request)
|
||||
{
|
||||
if (request == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(request));
|
||||
}
|
||||
|
||||
//return request.Headers.ContainsKey("X-Requested-With") &&
|
||||
// request.Headers["X-Requested-With"].Equals("XMLHttpRequest");
|
||||
|
||||
return request.Headers["X-Requested-With"] == "XMLHttpRequest" || request.Headers != null && request.Headers["X-Requested-With"] == "XMLHttpRequest";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取客户端IP
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetClientUserIp(this HttpContext context)
|
||||
{
|
||||
if (context == null) return "";
|
||||
var result = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
|
||||
if (string.IsNullOrEmpty(result))
|
||||
{
|
||||
result = context.Connection.RemoteIpAddress?.ToString();
|
||||
}
|
||||
if (string.IsNullOrEmpty(result))
|
||||
throw new Exception("获取IP失败");
|
||||
|
||||
if (result.Contains("::1"))
|
||||
result = "127.0.0.1";
|
||||
|
||||
result = result.Replace("::ffff:", "");
|
||||
result = result.Split(':')?.FirstOrDefault() ?? "127.0.0.1";
|
||||
result = IsIP(result) ? result : "127.0.0.1";
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否IP
|
||||
/// </summary>
|
||||
/// <param name="ip"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsIP(string ip)
|
||||
{
|
||||
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取登录用户id
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static long GetUId(this HttpContext context)
|
||||
{
|
||||
var uid = context.User.FindFirstValue(ClaimTypes.PrimarySid);
|
||||
return !string.IsNullOrEmpty(uid) ? long.Parse(uid) : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取登录用户名
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetName(this HttpContext context)
|
||||
{
|
||||
var uid = context.User?.Identity?.Name;
|
||||
|
||||
return uid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否是管理员
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsAdmin(this HttpContext context)
|
||||
{
|
||||
var userName = context.GetName();
|
||||
return userName == GlobalConstant.AdminRole;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ClaimsIdentity
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<ClaimsIdentity> GetClaims(this HttpContext context)
|
||||
{
|
||||
return context.User?.Identities;
|
||||
}
|
||||
//public static int GetRole(this HttpContext context)
|
||||
//{
|
||||
// var roleid = context.User.FindFirstValue(ClaimTypes.Role) ?? "0";
|
||||
|
||||
// return int.Parse(roleid);
|
||||
//}
|
||||
|
||||
public static string GetUserAgent(this HttpContext context)
|
||||
{
|
||||
return context.Request.Headers["User-Agent"];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取请求令牌
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetToken(this HttpContext context)
|
||||
{
|
||||
return context.Request.Headers["Authorization"];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取请求Url
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetRequestUrl(this HttpContext context)
|
||||
{
|
||||
return context != null ? context.Request.Path.Value : "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取请求参数
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetQueryString(this HttpContext context)
|
||||
{
|
||||
return context != null ? context.Request.QueryString.Value : "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取body请求参数
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetBody(this HttpContext context)
|
||||
{
|
||||
context.Request.EnableBuffering();
|
||||
//context.Request.Body.Seek(0, SeekOrigin.Begin);
|
||||
//using var reader = new StreamReader(context.Request.Body, Encoding.UTF8);
|
||||
////需要使用异步方式才能获取
|
||||
//return reader.ReadToEndAsync().Result;
|
||||
string body = string.Empty;
|
||||
var buffer = new MemoryStream();
|
||||
context.Request.Body.Seek(0, SeekOrigin.Begin);
|
||||
context.Request.Body.CopyToAsync(buffer);
|
||||
buffer.Position = 0;
|
||||
try
|
||||
{
|
||||
using StreamReader streamReader = new(buffer, Encoding.UTF8);
|
||||
body = streamReader.ReadToEndAsync().Result;
|
||||
}
|
||||
finally
|
||||
{
|
||||
buffer?.Dispose();
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取浏览器信息
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static ClientInfo GetClientInfo(this HttpContext context)
|
||||
{
|
||||
var str = context.GetUserAgent();
|
||||
var uaParser = Parser.GetDefault();
|
||||
ClientInfo c = uaParser.Parse(str);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置请求参数
|
||||
/// </summary>
|
||||
/// <param name="reqMethod"></param>
|
||||
/// <param name="context"></param>
|
||||
public static string GetRequestValue(this HttpContext context,string reqMethod)
|
||||
{
|
||||
string param= string.Empty;
|
||||
|
||||
if (HttpMethods.IsPost(reqMethod) || HttpMethods.IsPut(reqMethod) || HttpMethods.IsDelete(reqMethod))
|
||||
{
|
||||
param = context.GetBody();
|
||||
param = PwdRep().Replace(param, "***");
|
||||
}
|
||||
if (param.IsEmpty())
|
||||
{
|
||||
param = context.GetQueryString();
|
||||
}
|
||||
return param;
|
||||
}
|
||||
|
||||
[GeneratedRegex("(?<=\"password\":\")[^\",]*")]
|
||||
private static partial Regex PwdRep();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user