oauth-2.0 如何在WebAPI中执行AzureAd令牌身份验证时隐藏实际的ClientId

sqxo8psd  于 2022-10-31  发布在  其他
关注(0)|答案(1)|浏览(141)

我已经为我的webAPI配置了AzureAd令牌身份验证,但在swagger页面中,它显示的是实际的clientId值,但我不想向最终用户显示ClientId的实际值。这意味着在代码中,我可以硬编码,但在swagger页面中,我想显示一些虚拟值,这如何实现?在clientId文本框中,我想传递任何随机值,如“swaggerClient”

services.AddSwaggerGen(op =>
            {
                    var openApi = new OpenApiSecurityScheme
                    {
                        Flows = new OpenApiOAuthFlows
                        {
                            AuthorizationCode = new OpenApiOAuthFlow
                            {
                                AuthorizationUrl = "https://abcde.com",
                                Scopes = new Dictionary<string, string>
                                {
                                    { Scope, "mvc1"}
                                },
                                TokenUrl = "https://abcde.com/token"
                            }
                        },
                        In = ParameterLocation.Header,
                        Name = "Authorization",
                        Type = SecuritySchemeType.OAuth2
                    };

                    op.AddSecurityRequirement(new OpenApiSecurityRequirement
                                {
                                    {
                                          new OpenApiSecurityScheme
                                          {
                                              Reference = new OpenApiReference
                                              {
                                                  Type = ReferenceType.SecurityScheme,
                                                  Id = "oauth2"
                                              }
                                          },
                                         new string[] {}
                                    }
                                });

                    options.AddSecurityDefinition("oauth2", openApi);
                    options.OperationFilter<SecurityRequirementsOperationFilter>();

            });
3zwtqj6y

3zwtqj6y1#

Client_id不被隐藏,因为它不是OAuth规范RFC 6749 -OAuth 2.0授权框架所指示的秘密,并且它被暴露给资源所有者,并且必须与客户端秘密沿着用于客户端认证,并且秘密无论如何都被隐藏。
请检查是否可以通过将SchemaFilterIgnoreDataMember一起使用(类似于C# ASP.NET : Hide model properties from Swagger doc - DEV Community)来隐藏参数

  • 从getter和setter类获取客户端ID
[IgnoreDataMember]
 public string ClientId { set; get; }
  • 请使用ISchemaFilter来控制它。请添加新类。
using Microsoft.OpenApi.Models;
  using Swashbuckle.AspNetCore.SwaggerGen;
  using System.Linq;
  using System.Reflection;
  using System.Runtime.Serialization;

 namespace swaggertest
  {
   public class MySwaggerSchemaFilter : ISchemaFilter
       {
     public void Apply(OpenApiSchema schema, SchemaFilterContext context)
     {
         if (schema?.Properties == null)
         {
             return;
         }

         var ignoreDataMemberProperties = context.Type.GetProperties()
             .Where(t => t.GetCustomAttribute<IgnoreDataMemberAttribute>() != null);

         foreach (var ignoreDataMemberProperty in ignoreDataMemberProperties)
         {
             var propertyToHide = schema.Properties.Keys
                 .SingleOrDefault(x => x.ToLower() == ignoreDataMemberProperty.Name.ToLower());

             if (propertyToHide != null)
             {
                 schema.Properties.Remove(propertyToHide);
             }
         }
     }
   }
}
  • 然后在启动时指定
public void ConfigureServices(IServiceCollection services)
 {

 services.AddControllers();
 services.AddSwaggerGen(c =>
 {
     c.SwaggerDoc("v1", new OpenApiInfo { Title = "swaggertest", Version = "v1" });
     c.SchemaFilter<MySwaggerSchemaFilter>();
 });
 ...
}

或者按照kievu在github issue中建议的changing the DOM属性
你可以提出一个天蓝色的support requestswagger support关于相同的。

参考资料:

  1. Testing Azure AD-protected APIs, part 1: Swagger UI - Joonas W's blog
  2. How To Swagger Hide API Or Route Method – Guidelines |TheCodeBuzz

相关问题