中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

LindDotNetCore~授權中間件的介紹

回到目錄

LindDotNetCore中間件

大叔認識中間件就是主要對http請求進行攔截,然后添加(jia)具體個性化功(gong)能(neng)的(de)邏輯(ji),這種把(ba)請求切開,添加(jia)新邏輯(ji)的(de)方式(shi)一般稱為(wei)面(mian)向(xiang)方面(mian)的(de)邏輯(ji)AOP!

  1. 授權中間件
  2. 請求鏈跟蹤中間件
  3. 響應時間中間件

授權中間件

請求有效性的校驗

  • 授權參數
 /// <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; }
 }
  • 客戶端請求參數
 /// <summary>
 /// 從http請求發過來的授權實體
 /// </summary>
 public class AuthorizationRequestInfo
 {
     public string ApplicationId { get; set; }
     public string Timestamp { get; set; }
     public string Sinature { get; set; }
 }
  • 請求攔截器,處理請求有效性,對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);
 }
  • 為開發人員提供友好的擴展方法,用來注冊中間件
 /// <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();
 }

請求鏈跟蹤中間件

記錄請求經過的(de)整個過程(cheng),對(dui)于(yu)多api相(xiang)互調用(yong)(yong)的(de)場景比較有用(yong)(yong)

響應時間中間件

記錄大于指定時間的請求信息,方便做性能整體的提升
回到目錄

posted @ 2018-01-09 22:40  張占嶺  閱讀(897)  評論(0)    收藏  舉報