转移.net core 3.1,为.NET 5做准备

This commit is contained in:
ÂëÉñ
2020-10-22 14:59:36 +08:00
parent fd9bca23a7
commit a35d596237
1080 changed files with 175912 additions and 185681 deletions

View File

@@ -0,0 +1,57 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using OpenAuth.App;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace OpenAuth.WebApi.Model
{
/// <summary>
/// swagger请求的时候如果是Identity方式自动加授权方式
/// </summary>
public class AuthResponsesOperationFilter : IOperationFilter
{
private IOptions<AppSetting> _appConfiguration;
public AuthResponsesOperationFilter(IOptions<AppSetting> appConfiguration)
{
_appConfiguration = appConfiguration;
}
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (!_appConfiguration.Value.IsIdentityAuth)
{
return;
}
var anonymous = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
.Union(context.MethodInfo.GetCustomAttributes(true))
.OfType<AllowAnonymousAttribute>().Any();
if (!anonymous)
{
var security = new List<OpenApiSecurityRequirement>();
security.Add(new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "oauth2"
}
},
new[] { "openauthapi" }
}
});
operation.Security = security;
// operation.Security = new List<OpenApiSecurityRequirement>
// {
// new Dictionary<string, IEnumerable<string>> {{"oauth2", new[] { "openauthapi" } }}
// };
}
}
}
}

View File

@@ -0,0 +1,54 @@
// <copyright file="GlobalHttpHeaderOperationFilter.cs" company="openauth.me">
// Copyright (c) 2019 openauth.me. All rights reserved.
// </copyright>
// <author>www.cnblogs.com/yubaolee</author>
// <date>2019-01-05</date>
// <summary>在swagger界面加上http header</summary>
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using OpenAuth.App;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace OpenAuth.WebApi.Model
{
public class GlobalHttpHeaderOperationFilter : IOperationFilter
{
private IOptions<AppSetting> _appConfiguration;
public GlobalHttpHeaderOperationFilter(IOptions<AppSetting> appConfiguration)
{
_appConfiguration = appConfiguration;
}
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
//如果是Identity认证方式不需要界面添加x-token得输入框
if (_appConfiguration.Value.IsIdentityAuth)
return;
if (operation.Parameters == null)
{
operation.Parameters = new List<OpenApiParameter>();
}
var actionAttrs = context.ApiDescription.ActionDescriptor.EndpointMetadata;
var isAnony = actionAttrs.Any(a => a.GetType() == typeof(AllowAnonymousAttribute));
//不是匿名则添加默认的X-Token
if (!isAnony)
{
operation.Parameters.Add(new OpenApiParameter
{
Name = Define.TOKEN_NAME,
In = ParameterLocation.Header,
Description = "当前登录用户登录token",
Required = false
});
}
}
}
}

View File

@@ -0,0 +1,63 @@
using System.Reflection;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using OpenAuth.App;
using OpenAuth.App.Interface;
using OpenAuth.Repository.Domain;
namespace OpenAuth.WebApi.Model
{
public class OpenAuthFilter : IActionFilter
{
private readonly IAuth _authUtil;
private readonly SysLogApp _logApp;
public OpenAuthFilter(IAuth authUtil, SysLogApp logApp)
{
_authUtil = authUtil;
_logApp = logApp;
}
public void OnActionExecuting(ActionExecutingContext context)
{
var description =
(Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor;
var Controllername = description.ControllerName.ToLower();
var Actionname = description.ActionName.ToLower();
//匿名标识
var authorize = description.MethodInfo.GetCustomAttribute(typeof(AllowAnonymousAttribute));
if (authorize != null)
{
return;
}
if (!_authUtil.CheckLogin())
{
context.HttpContext.Response.StatusCode = 401;
context.Result = new JsonResult(new Response
{
Code = 401,
Message = "认证失败,请提供认证信息"
});
return;
}
_logApp.Add(new SysLog
{
Content = $"用户访问",
Href = $"{Controllername}/{Actionname}",
CreateName = _authUtil.GetUserName(),
CreateId = _authUtil.GetCurrentUser().User.Id,
TypeName = "访问日志"
});
}
public void OnActionExecuted(ActionExecutedContext context)
{
return;
}
}
}

View File

@@ -0,0 +1,24 @@

using Microsoft.Extensions.Logging;
namespace OpenAuth.WebApi.Model
{
/// <summary>
/// 从3.0开始Startup ConfigureServices中不能使用ILogger需要扩展
/// </summary>
public class StartupLogger
{
private readonly ILogger<StartupLogger> _logger;
public StartupLogger(ILogger<StartupLogger> logger)
{
_logger = logger;
}
public void LogInformation(string message)
{
_logger.LogInformation(message);
}
}
}