C# swagger ui增加訪問限制方式
C# swagger ui增加訪問限制
swagger 頁面是個(gè)很好的接口文檔,可以直接給三方系統(tǒng)查看參考,如果所有人都能訪問有一些風(fēng)險(xiǎn)
只需要三部解決。不廢話,直接上代碼
1、增加SwaggerBasicAuthMiddleware.cs類
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using VOL.Core.Configuration; using VOL.Core.DBManager; using VOL.Core.Extensions; using VOL.Entity.DomainModels; public class SwaggerBasicAuthMiddleware { private readonly RequestDelegate next; public SwaggerBasicAuthMiddleware(RequestDelegate next) { this.next = next; } /// <summary> /// 驗(yàn)證賬號密碼 /// </summary> /// <param name="userName">賬號</param> /// <param name="passWorld">密碼</param> /// <returns></returns> public bool Login(string userName, string passWorld) { var EncryptPwd = passWorld.EncryptDES(AppSetting.Secret.User);//密碼加密 return DBServerProvider.DbContext.Set<Sys_User>().Where(s => s.UserName == userName && s.UserPwd == EncryptPwd).Any(); } public async Task InvokeAsync(HttpContext context) { if (context.Request.Path.StartsWithSegments("/swagger")) { string authHeader = context.Request.Headers["Authorization"]; if (authHeader != null && authHeader.StartsWith("Basic")) { // Get the credentials from request header var header = AuthenticationHeaderValue.Parse(authHeader); var inBytes = Convert.FromBase64String(header.Parameter); var credentials = Encoding.UTF8.GetString(inBytes).Split(':'); var username = credentials[0]; var password = credentials[1]; // validate credentials if (Login(username, password)) { await next.Invoke(context).ConfigureAwait(false); return; } } //告知服務(wù)器端需要進(jìn)行Basic認(rèn)證 context.Response.Headers["WWW-Authenticate"] = "Basic"; context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; } else { await next.Invoke(context).ConfigureAwait(false); } } }
可以將login方法換成你需要的校驗(yàn),我這里直接用系統(tǒng)的賬號密碼做的校驗(yàn)
2、增加MiddlerwareExtention.cs類
using Microsoft.AspNetCore.Builder; /// <summary> /// 中間件拓展類 /// </summary> public static class MiddlerwareExtention { public static IApplicationBuilder UseSwaggerBasicAuth(this IApplicationBuilder app) { return app.UseMiddleware<SwaggerBasicAuthMiddleware>(); } }
3、在Startup.cs的Configure方法
注入app.UseSwaggerBasicAuth();
齊活~~~~~~~!
再次運(yùn)行看效果
輸入對應(yīng)的賬號密碼即可。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
c# winform時(shí)鐘的實(shí)現(xiàn)代碼
這篇文章主要介紹了c# winform時(shí)鐘的實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2014-01-01Winform開發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式
這篇文章介紹了Winform開發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09Unity UGUI實(shí)現(xiàn)簡單拖拽圖片功能
這篇文章主要為大家詳細(xì)介紹了Unity UGUI實(shí)現(xiàn)簡單拖拽圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06Unity實(shí)現(xiàn)粒子光效導(dǎo)出成png序列幀
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)粒子光效導(dǎo)出成png序列幀,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03C#判斷一個(gè)類是否實(shí)現(xiàn)了某個(gè)接口3種實(shí)現(xiàn)方法
這篇文章主要介紹了C#判斷一個(gè)類是否實(shí)現(xiàn)了某個(gè)接口3種實(shí)現(xiàn)方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06C#基礎(chǔ)之?dāng)?shù)組排序、對象大小比較實(shí)現(xiàn)代碼
C#基礎(chǔ)之?dāng)?shù)組排序、對象大小比較實(shí)現(xiàn)代碼,學(xué)習(xí)c#的朋友可以參考下。2011-08-08