如何使用 Swashbuckle 在 Swagger 中隐藏密码

Tor*_*sen 2 c# swashbuckle asp.net-core

我们在 API 上使用 Swashbuckle,对于其中一种方法,我们提供登录。如何在 Swagger UI 中屏蔽密码?

[HttpGet]
[Produces("text/plain")]
public async Task<string> Login(string userId, string password)
Run Code Online (Sandbox Code Playgroud)

我发现 Swagger 显然支持密码 fromat。但这不是 C# 代码。我不知道该把它放在哪里

@Parameter(schema = @Schema(type = "string", format = "password")) 
Run Code Online (Sandbox Code Playgroud)

D-S*_*hih 6

我们可以尝试将DataType属性与DataType.Password枚举一起使用,放入我们想要屏蔽的参数中。

[HttpGet]
[Produces("text/plain")]
public async Task<string> Login(string userId,[DataType(DataType.Password)]
 string password)
Run Code Online (Sandbox Code Playgroud)

然后我们可以得到如下的 mask 参数

在此输入图像描述

此修改可能会在不使用 DataType.Password #521v2.5.0中讨论的版本之后起作用

注意:使用命名空间中的属性和枚举System.ComponentModel.DataAnnotations