C# swagger ui增加訪問(wèn)限制方式
C# swagger ui增加訪問(wèn)限制

swagger 頁(yè)面是個(gè)很好的接口文檔,可以直接給三方系統(tǒng)查看參考,如果所有人都能訪問(wèn)有一些風(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)證賬號(hào)密碼
/// </summary>
/// <param name="userName">賬號(hào)</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)的賬號(hào)密碼做的校驗(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)行看效果

輸入對(duì)應(yīng)的賬號(hào)密碼即可。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Unity 實(shí)現(xiàn)框選游戲戰(zhàn)斗單位的思路詳解
這篇文章主要介紹了Unity 如何實(shí)現(xiàn)框選游戲戰(zhàn)斗單位,本文簡(jiǎn)單介紹如何實(shí)現(xiàn)即時(shí)戰(zhàn)略游戲中框選戰(zhàn)斗單位的功能,需要的朋友可以參考下2022-12-12
C# BinaryReader實(shí)現(xiàn)讀取二進(jìn)制文件
在 C# 以二進(jìn)制形式讀取數(shù)據(jù)時(shí)使用的是 BinaryReader 類。本文介紹了C# BinaryReader實(shí)現(xiàn)讀取二進(jìn)制文件,感興趣的可以了解一下2021-06-06
VS2019配置OpenCV4.1.0詳細(xì)教程與測(cè)試代碼(推薦)
這篇文章主要介紹了VS2019配置OpenCV4.1.0詳細(xì)教程與測(cè)試代碼,本文通過(guò)截圖實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
C#實(shí)現(xiàn)從Word文檔中提取指定頁(yè)面的方法詳解
這篇文章主要為大家詳細(xì)介紹了C#如何使用 代碼實(shí)現(xiàn)提取 Word 文檔中的指定頁(yè)面,文檔的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2026-02-02
在C#中調(diào)用Python腳本實(shí)現(xiàn)跨語(yǔ)言功能集成
隨著現(xiàn)代應(yīng)用程序的復(fù)雜性和多樣性不斷增加,跨語(yǔ)言集成已成為一種常見(jiàn)的開(kāi)發(fā)實(shí)踐,C# 和 Python 是兩種廣泛使用的編程語(yǔ)言,本文將詳細(xì)介紹如何在 C# 中調(diào)用 Python 腳本,并通過(guò)傳遞參數(shù)實(shí)現(xiàn)功能的跨語(yǔ)言集成,需要的朋友可以參考下2026-03-03
DevExpress GridControl實(shí)現(xiàn)根據(jù)RowIndex和VisibleColumnsIndex來(lái)獲取單元格
這篇文章主要介紹了DevExpress GridControl實(shí)現(xiàn)根據(jù)RowIndex和VisibleColumnsIndex來(lái)獲取單元格值,需要的朋友可以參考下2014-08-08
C#使用委托實(shí)現(xiàn)的快速排序算法實(shí)例
這篇文章主要介紹了C#使用委托實(shí)現(xiàn)的快速排序算法,實(shí)例分析了C#委托機(jī)制與快速排序算法的實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-07-07

