澳门1495娱乐LindDotNetCore~授权中间件的介绍。LindDotNetCore~授权中间件的介绍。

回目录

回目录

LindDotNetCore中间件

大爷认识中件就是关键对http请求进行阻挠,然后上加具体个性化功能的逻辑,这种把要切开,添加新逻辑的法门相似叫面向方面的逻辑AOP!

  1. 授权中间件
  2. 呼吁求链跟踪中间件
  3. 应时间中件

    #### 授权中间件

    求中的校验

  4. 授权参数

     /// <summary>
     /// 授权配置
     /// </summary>
     public class AuthorizationConfig
     {
     /// <summary>
     /// 统一密钥
     /// </summary>
     public string EncryptKey { get; set; }
     /// <summary>
     /// 过期时间秒数
     /// </summary>
     public int ExpiredSecond { get; set; }
     /// <summary>
     /// 被授权的app
     /// </summary>
     public string[] AppList { get; set; }
     }
    
  5. 客户端请求参数

     /// <summary>
     /// 从http请求发过来的授权实体
     /// </summary>
     public class AuthorizationRequestInfo
     {
     public string ApplicationId { get; set; }
     public string Timestamp { get; set; }
     public string Sinature { get; set; }
     }
    
  6. 要拦截器,处理要中,对app,过期日,加密艺术开展校验

     string computeSinature = MD5($"{requestInfo.ApplicationId}-{requestInfo.Timestamp}-{_options.EncryptKey}");
     double tmpTimestamp;
     if (computeSinature.Equals(requestInfo.Sinature) &&
     double.TryParse(requestInfo.Timestamp, out tmpTimestamp))
     {
     if (ValidateExpired(tmpTimestamp, _options.ExpiredSecond))
     {
         await ReturnTimeOut(context);
     }
     else
     {
         await ValidateApp(context, requestInfo.ApplicationId);
     }
     }
     else
     {
     await ReturnNotAuthorized(context);
     }
    
  7. 也开发人员提供温馨之扩大方法,用来注册中件

 /// <summary>
 /// 注册授权服务-step1
 /// </summary>
 /// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
 /// <param name="configureOptions">A delegate to configure the <see cref="ResponseCompressionOptions"/>.</param>
 /// <returns></returns>
 public static IServiceCollection AddLindAuthrization(this IServiceCollection services, Action<AuthorizationConfig> configureOptions = null)
 {
     if (services == null)
     {
         throw new ArgumentNullException(nameof(services));
     }
     var options = new AuthorizationConfig();
     configureOptions?.Invoke(options);
     ObjectMapper.MapperTo(options, ConfigFileHelper.Get<AuthorizationConfig>());
     services.AddSingleton(options);
     return services;
 }

 /// <summary>
 /// 使用授权中间件-step2
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static IApplicationBuilder UseLindAuthrization(this IApplicationBuilder builder)
 {
     if (builder == null)
     {
         throw new ArgumentNullException(nameof(builder));
     }
     var options = builder.ApplicationServices.GetService<AuthorizationConfig>();
     return builder.UseMiddleware<AuthorizationMiddleware>(options);
 }
  • 以授权中间件Startup中注册

 // 注册服务
 services.AddLindAuthrization(options =>
 {
     options.EncryptKey = "abc123";
     options.ExpiredSecond = 50;
     options.AppList = new string[] { "1", "2", "3" };
 });
 // 注册中间件 
 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {
     if (env.IsDevelopment())
     {
         app.UseDeveloperExceptionPage();
     }
     app.UseLindAuthrization();
     app.UseMvc();
 }

LindDotNetCore中间件

伯父认识中件就是关键对http请求进行拦,然后上加具体个性化功能的逻辑,这种将要切开,添加新逻辑的法一般叫面向方面的逻辑AOP!

  1. 授权中间件
  2. 恳请求链跟踪中间件
  3. 响应时间中件

    #### 授权中间件

    伸手中的校验

  4. 授权参数

     /// <summary>
     /// 授权配置
     /// </summary>
     public class AuthorizationConfig
     {
     /// <summary>
     /// 统一密钥
     /// </summary>
     public string EncryptKey { get; set; }
     /// <summary>
     /// 过期时间秒数
     /// </summary>
     public int ExpiredSecond { get; set; }
     /// <summary>
     /// 被授权的app
     /// </summary>
     public string[] AppList { get; set; }
     }
    
  5. 客户端请求参数

     /// <summary>
     /// 从http请求发过来的授权实体
     /// </summary>
     public class AuthorizationRequestInfo
     {
     public string ApplicationId { get; set; }
     public string Timestamp { get; set; }
     public string Sinature { get; set; }
     }
    
  6. 央拦截器,处理要中,对app,过期岁月,加密方展开校验

     string computeSinature = MD5($"{requestInfo.ApplicationId}-{requestInfo.Timestamp}-{_options.EncryptKey}");
     double tmpTimestamp;
     if (computeSinature.Equals(requestInfo.Sinature) &&
     double.TryParse(requestInfo.Timestamp, out tmpTimestamp))
     {
     if (ValidateExpired(tmpTimestamp, _options.ExpiredSecond))
     {
         await ReturnTimeOut(context);
     }
     else
     {
         await ValidateApp(context, requestInfo.ApplicationId);
     }
     }
     else
     {
     await ReturnNotAuthorized(context);
     }
    
  7. 为开发人员提供好之恢宏方法,用来报中件

 /// <summary>
 /// 注册授权服务-step1
 /// </summary>
 /// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
 /// <param name="configureOptions">A delegate to configure the <see cref="ResponseCompressionOptions"/>.</param>
 /// <returns></returns>
 public static IServiceCollection AddLindAuthrization(this IServiceCollection services, Action<AuthorizationConfig> configureOptions = null)
 {
     if (services == null)
     {
         throw new ArgumentNullException(nameof(services));
     }
     var options = new AuthorizationConfig();
     configureOptions?.Invoke(options);
     ObjectMapper.MapperTo(options, ConfigFileHelper.Get<AuthorizationConfig>());
     services.AddSingleton(options);
     return services;
 }

 /// <summary>
 /// 使用授权中间件-step2
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static IApplicationBuilder UseLindAuthrization(this IApplicationBuilder builder)
 {
     if (builder == null)
     {
         throw new ArgumentNullException(nameof(builder));
     }
     var options = builder.ApplicationServices.GetService<AuthorizationConfig>();
     return builder.UseMiddleware<AuthorizationMiddleware>(options);
 }
  • 使用授权中间件Startup中注册

 // 注册服务
 services.AddLindAuthrization(options =>
 {
     options.EncryptKey = "abc123";
     options.ExpiredSecond = 50;
     options.AppList = new string[] { "1", "2", "3" };
 });
 // 注册中间件 
 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {
     if (env.IsDevelopment())
     {
         app.UseDeveloperExceptionPage();
     }
     app.UseLindAuthrization();
     app.UseMvc();
 }

恳请求链跟踪中间件

笔录请求经过的万事经过,对于多api相互调用的场景比较有因此

央求链跟踪中间件

记录请求经过的全方位经过,对于多api相互调用的状况比较起因此

一呼百应时间中件

记录大于指定时间之请信息,方便做性能完全的升官
回目录

响应时间中件

记录大于指定时间之呼吁信息,方便做性能完全的升级
回来目录

相关文章