【问题标题】:Swagger UI - Oauth password flow, retrieve and add token to authorized requestsSwagger UI - Oauth 密码流,检索和添加令牌到授权请求
【发布时间】:2017-12-26 08:47:37
【问题描述】:

我刚刚开始在 MVC 中为 ASP.Net API Web 项目使用 Swagger UI。 除了身份验证之外,我理解的大部分部分都正确。

我正在使用 OAuth ASP.Net 身份。以下是我的设置:

SwaggerConfig.cs

         c.OAuth2("oauth2")
                        .Description("OAuth2 Implicit Grant")
                        .Flow("password")
                        .AuthorizationUrl("/api/Account/ExternalLogin")
                        .TokenUrl("/Token")
                        .Scopes(scopes =>
                        {
                            scopes.Add("values:read", "Read access to protected resources");
                            scopes.Add("values:write", "Write access to protected resources");
                        });

         c.OperationFilter<AssignOAuth2SecurityRequirements>();

AssignOAuth2SecurityRequirements.cs

internal class AssignOAuth2SecurityRequirements : IOperationFilter
    {
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            var authorizeAttributes = apiDescription.ActionDescriptor.GetCustomAttributes<AuthorizeAttribute>();

            if (!authorizeAttributes.Any())
                return;

            if (operation.security == null)
                operation.security = new List<IDictionary<string, IEnumerable<string>>>();

            var oAuthRequirements = new Dictionary<string, IEnumerable<string>>
            {
                { "oauth2", Enumerable.Empty<string>() }
            };

            operation.security.Add(oAuthRequirements);

        }
    }

index.html

<script>
window.onload = function() {

  // Build a system
  const ui = SwaggerUIBundle({
      url: "http://localhost:17527/swagger/docs/v1",
    dom_id: '#swagger-ui',
    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  })

  window.ui = ui
}
</script>

在授权APP页面的/Token请求时。

但是当我尝试访问 values 端点时,它会引发错误。

原因是请求缺少应该进入标头的承载令牌

我已经尝试了一些解决方案,但无法解决。

提前致谢。

【问题讨论】:

    标签: c# asp.net-web-api oauth swagger swagger-ui


    【解决方案1】:

    我最近也试过这个,你必须将 [Authorize] 属性放在方法级别而不是控制器级别,然后它将起作用并在每个请求中发送 Bearer 令牌。

    【讨论】:

      【解决方案2】:

      您也可以像这样同时检查 ActionDescriptor 和 ControllerDescriptor。它会阻止您将 Authorize 属性放在所有控制器方法上。

       public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
       {
              var authorizeAttributes = apiDescription.ActionDescriptor.GetCustomAttributes<AuthorizeAttribute>().Any() 
                                               ? apiDescription.ActionDescriptor.GetCustomAttributes<AuthorizeAttribute>()
                                               : apiDescription.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<AuthorizeAttribute>();
      
              if (!authorizeAttributes.Any())
                  return;
      
              if (operation.security == null)
                  operation.security = new List<IDictionary<string, IEnumerable<string>>>();
      
              var oAuthRequirements = new Dictionary<string, IEnumerable<string>>
              {
                  { "oauth2", Enumerable.Empty<string>() }
              };
      
              operation.security.Add(oAuthRequirements);
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-23
        • 1970-01-01
        • 2019-10-01
        • 2016-08-29
        • 2011-11-16
        • 2020-05-10
        • 1970-01-01
        • 2018-06-11
        相关资源
        最近更新 更多