当前位置:网站首页>.NET Custom Middleware
.NET Custom Middleware
2022-08-11 03:52:00 【JDBogdan】
/**
* 1.Authentication extension class
*/
public static class CustomAuthenticationAppBuilderExtensions
{
public static IApplicationBuilder UseCustomAuthentication(this IApplicationBuilder app, IConfiguration configuration)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
var authServerAuthority = configuration["AuthServer:Authority"];
return authServerAuthority.Contains("uihcloud.cn")
? app.UseAuthentication().UseMiddleware<CustomAuthenticationMiddleware>()
: app.UseAuthentication();
}
}
/**
* 1.Custom authentication middleware
*/
public class CustomAuthenticationMiddleware
{
private readonly RequestDelegate _next;
private readonly IdentityUserManager _identityUserManager;
private readonly IUserService _uapUserService;
public CustomAuthenticationMiddleware(RequestDelegate next, IdentityUserManager identityUserManager, IUserService uapUserService)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
_identityUserManager = identityUserManager ?? throw new ArgumentNullException(nameof(identityUserManager));
_uapUserService = uapUserService ?? throw new ArgumentNullException(nameof(uapUserService));
}
public async Task Invoke(HttpContext context)
{
if (context.Request.Headers.TryGetValue("Authorization", out var authorization))
{
var token = authorization.ToString().Replace(JwtBearerDefaults.AuthenticationScheme, "").Trim();
try
{
var claims = await GetClaimsAsync(token);
var claimsIdentity = new ClaimsIdentity(claims);
context.User = new ClaimsPrincipal(claimsIdentity);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
await _next(context);
}
private async Task<IEnumerable<Claim>> GetClaimsAsync(string token)
{
var authorization = $"{JwtBearerDefaults.AuthenticationScheme} {token}";
var uapUserInfo = await _uapUserService.GetUserInfoAsync(authorization);
var identityUser = await _identityUserManager.FindByNameAsync(uapUserInfo.Name);
return identityUser == null
? new List<Claim>()
: new List<Claim>
{
new(AbpClaimTypes.UserId, $"{identityUser.Id}"),
new(AbpClaimTypes.UserName, $"{uapUserInfo.UserName}"),
new(AbpClaimTypes.Name, $"{uapUserInfo.Name}"),
new(AbpClaimTypes.SurName, $"{identityUser.Surname}"),
new(AbpClaimTypes.PhoneNumber, $"{uapUserInfo.UserPhone}"),
new(AbpClaimTypes.Email, $"{uapUserInfo.UserEmail}"),
new(AbpClaimTypes.TenantId, $"{identityUser.TenantId}"),
new("appId", $"{uapUserInfo.AppId}"),
new("exp", $"{uapUserInfo.Exp}"),
new("iat", $"{uapUserInfo.Iat}")
}.WhereNotNull().ToList();
}
}
=========================================================================================
/**
* 1.Authorization extension class
*/
public static class CustomAuthorizationAppBuilderExtensions
{
public static IApplicationBuilder UseCustomAuthorization(this IApplicationBuilder app, IConfiguration configuration)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
var authServerAuthority = configuration["AuthServer:Authority"];
return authServerAuthority.Contains("uihcloud.cn")
? app.UseAuthorization().UseMiddleware<CustomAuthorizationMiddleware>()
: app.UseAuthorization();
}
}
/**
* 1.Custom authorization middleware
*/
public class CustomAuthorizationMiddleware
{
private readonly RequestDelegate _next;
private readonly IConfiguration _configuration;
public CustomAuthorizationMiddleware(RequestDelegate next, IConfiguration configuration)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
}
public async Task Invoke(HttpContext context)
{
if (context.User.Claims.Any() == false)
{
await _next(context);
return;
}
try
{
//var appId = context.User.Claims.FirstOrDefault(_ => _.Type == "appId")?.Value;
//var redisConfiguration = _configuration["Redis:Configuration"];
//using var redis = await ConnectionMultiplexer.ConnectAsync(redisConfiguration);
//var db = redis.GetDatabase();
//var token = db.HashGet("5fb9ed690816442cad2e18e5a769fe06", appId).ToString();
}
catch (Exception e)
{
Console.WriteLine($"Authorization failed {e.Message}");
return;
}
await _next(context);
}
}
=========================================================================================
/**
* 1.Custom authorization policy middleware
*/
public static class CustomEndpointsAppBuilderExtensions
{
public static IApplicationBuilder UseCustomEndpoints(this IApplicationBuilder app, IConfiguration configuration)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
var authServerAuthority = configuration["AuthServer:Authority"];
return authServerAuthority.Contains("uihcloud.cn")
? app.UseEndpoints(options =>
{
options.MapControllers().RequireAuthorization("ApiScope");
})
: app.UseEndpoints(options =>
{
options.MapControllers();
});
}
}
=========================================================================================
/**
* 使用
*/
[DependsOn(
typeof(BookHttpApiModule),
typeof(AbpAutofacModule),
typeof(AbpAspNetCoreMvcUiMultiTenancyModule),
typeof(BookApplicationModule),
typeof(BookEntityFrameworkCoreDbMigrationsModule),
typeof(AbpAspNetCoreSerilogModule),
typeof(AbpSwashbuckleModule),
typeof(AbpBlobStoringMinioModule),
typeof(AbpEventBusRabbitMqModule),
typeof(AbpBackgroundJobsQuartzModule)
)]
public class BookHttpApiHostModule : AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
PreConfigure<AbpQuartzOptions>(options =>
{
options.Configurator = configure =>
{
configure.UsePersistentStore(storeOptions =>
{
storeOptions.UseProperties = false;
storeOptions.UseJsonSerializer();
storeOptions.UsePostgres(configuration.GetConnectionString(QuartzDbProperties.ConnectionStringName));
});
};
});
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
var hostingEnvironment = context.Services.GetHostingEnvironment();
ConfigureMaxRequestBodySize();
ConfigureAuthorization(context);
ConfigureConventionalControllers();
ConfigureAuthentication(context, configuration);
ConfigureLocalization();
ConfigureCache(configuration);
ConfigureVirtualFileSystem(context);
ConfigureCors(context, configuration);
ConfigureSwaggerServices(context, configuration);
ConfigureAuditLog();
ConfigureMinio(configuration)
context.Services.AddHttpContextAccessor();
}
private void ConfigureMinio(IConfiguration configuration)
{
Configure<AbpBlobStoringOptions>(options =>
{
options.Containers.ConfigureAll((containerName, containerConfiguration) =>
{
containerConfiguration.UseMinio(minio =>
{
minio.EndPoint = configuration["EndPoint"];
minio.AccessKey = configuration["AccessKey"];
minio.SecretKey = configuration["SecretKey"];
minio.BucketName = configuration["BucketName"];
});
});
});
}
private void ConfigureAuditLog()
{
Configure<AbpAuditingOptions>(options =>
{
options.IsEnabled = true;
options.ApplicationName = "book";
options.EntityHistorySelectors.AddAllEntities();
});
}
private void ConfigureMaxRequestBodySize()
{
Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = 1258291200;
});
Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = 1258291200;
});
//解决Multipart body length limit 134217728 exceeded
Configure<FormOptions>(options =>
{
options.ValueLengthLimit = int.MaxValue;
options.MultipartBodyLengthLimit = int.MaxValue;
options.MemoryBufferThreshold = int.MaxValue;
});
}
private void ConfigureCache(IConfiguration configuration)
{
Configure<AbpDistributedCacheOptions>(options => { options.KeyPrefix = "Book:"; });
}
private void ConfigureVirtualFileSystem(ServiceConfigurationContext context)
{
var hostingEnvironment = context.Services.GetHostingEnvironment();
if (hostingEnvironment.IsDevelopment())
{
Configure<AbpVirtualFileSystemOptions>(options =>
{
options.FileSets.ReplaceEmbeddedByPhysical<AbpModule>(
Path.Combine(hostingEnvironment.ContentRootPath,
$"..{Path.DirectorySeparatorChar}Book.Domain.Shared"));
}
}
private void ConfigureConventionalControllers()
{
Configure<AbpAspNetCoreMvcOptions>(options =>
{
options.ConventionalControllers.Create(typeof(BookApplicationModule).Assembly);
});
}
private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
{
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
var authority = configuration["AuthServer:Authority"];
options.Authority = authority;
options.RequireHttpsMetadata = authority.StartsWith("https://");
options.TokenValidationParameters = new TokenValidationParameters { ValidateAudience = false };
});
}
private void ConfigureAuthorization(ServiceConfigurationContext context)
{
context.Services.AddAuthorization(options =>
{
options.AddPolicy("ApiScope", builder =>
{
builder.RequireClaim(ClaimTypes.NameIdentifier);
});
});
}
private void ConfigureLocalization()
{
Configure<AbpLocalizationOptions>(options =>
{
options.Languages.Add(new LanguageInfo("en", "en", "English"));
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文"));
options.Languages.Add(new LanguageInfo("zh-Hant", "zh-Hant", "繁體中文"));
});
}
private void ConfigureRedis(ServiceConfigurationContext context, IConfiguration configuration)
{
if (configuration["ASPNETCORE_ENVIRONMENT"] != "Development")
{
var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
context.Services
.AddDataProtection()
.PersistKeysToStackExchangeRedis(redis, "Book-Protection-Keys");
}
}
private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
{
context.Services.AddCors(options =>
{
options.AddPolicy("Default", builder =>
{
builder
.WithOrigins(
configuration["App:CorsOrigins"]
.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(o => o.RemovePostFix("/"))
.ToArray()
)
.WithAbpExposedHeaders()
.SetIsOriginAllowedToAllowWildcardSubdomains()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
}
private static void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
{
context.Services.AddSwaggerGen(
options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Book API", Version = "v1" });
options.DocInclusionPredicate((_, _) => true);
options.AddSecurityDefinition(JwtBearerDefaults.AuthenticationScheme, new OpenApiSecurityScheme
{
Name = "Authorization",
Description = "Specify the authorization token.",
Scheme = JwtBearerDefaults.AuthenticationScheme,
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = JwtBearerDefaults.AuthenticationScheme
}
},
Array.Empty<string>()
}
});
});
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();
var env = context.GetEnvironment();
var cfg = context.GetConfiguration();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
var supportedCultures = new[]
{
new CultureInfo("zh-Hans"),
new CultureInfo("en"),
};
app.UseAbpRequestLocalization(options =>
{
options.DefaultRequestCulture = new RequestCulture("zh-Hans");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
};
});
if (!env.IsDevelopment())
{
app.UseErrorPage();
}
app.UseCorrelationId();
app.UseVirtualFiles();
app.UseRouting();
app.UseCors("Default");
app.UseSignalrAuthorization();
app.UseCustomAuthentication(cfg);
app.UseCustomAuthorization(cfg);
app.UseCustomEndpoints(cfg);
app.UseSwagger();
app.UseAbpSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Book API");
var configuration = context.GetConfiguration();
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
});
app.UseAuditing();
app.UseAbpSerilogEnrichers();
app.UseConfiguredEndpoints();
}
}
边栏推荐
- 你不知道的 console.log 替代品
- typedef定义结构体数组类型
- A large horse carries 2 stone of grain, a middle horse carries 1 stone of grain, and two ponies carry one stone of grain. It takes 100 horses to carry 100 stone of grain. How to distribute it?
- App Basic Framework Construction丨Log Management - KLog
- What is ensemble learning in machine learning?
- Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods
- Paper Accuracy - 2017 CVPR "High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis"
- 大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?
- Use jackson to parse json data in detail
- Callable实现多线程
猜你喜欢
[FPGA] Design Ideas - I2C Protocol
Differences and connections between distributed and clustered
作业8.10 TFTP协议 下载功能
【FPGA】day21- moving average filter
轮转数组问题:如何实现数组“整体逆序,内部有序”?“三步转换法”妙转数组
你不知道的 console.log 替代品
STC8H开发(十五): GPIO驱动Ci24R1无线模块
多商户商城系统功能拆解26讲-平台端分销设置
Qnet Weak Network Test Tool Operation Guide
STC8H development (15): GPIO drive Ci24R1 wireless module
随机推荐
typedef defines the structure array type
What is ensemble learning in machine learning?
LeetCode刷题第10天字符串系列之《125回文串验证》
es-head插件插入查询以及条件查询(五)
【FPGA】设计思路——I2C协议
Interchangeable Measurement Techniques - Geometric Errors
高校就业管理系统设计与实现
DNS separation resolution and intelligent resolution
Audio codec, using FAAC to implement AAC encoding
Build Zabbix Kubernetes cluster monitoring platform
The thirteenth day of learning programming
Power Cabinet Data Monitoring RTU
What are port 80 and port 443?What's the difference?
Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)
荣威imax8ev魔方电池安全感,背后隐藏着哪些黑化膨胀?
轮转数组问题:如何实现数组“整体逆序,内部有序”?“三步转换法”妙转数组
The development of the massage chair control panel makes the massage chair simple and intelligent
Leetcode 108. 将有序数组转换为二叉搜索树
MYSQLg advanced ------ return table
AI + medical: for medical image recognition using neural network analysis