.NET Core 2.2,Swagger和[Authorize]属性

时间:2019-07-18 14:20:36

标签: .net-core swagger swagger-ui

我有一个.NET Core 2.2应用程序,并且在应用程序中添加了大张旗鼓。

ConfigureServices

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info
            {
                Title = "Tablet Management",
                Version = "v1",
                Contact = new Contact
                {
                    Name = "Oleg ",
                    Email = "o@3mdsolutions.com"
                },
            });

            // Set the comments path for the Swagger JSON and UI.
            var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
            var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
            c.IncludeXmlComments(xmlPath);
        });

Configure

    // Enable middleware to serve generated Swagger as a JSON endpoint.
    app.UseSwagger();

    // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), 
    // specifying the Swagger JSON endpoint.
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "Tablet Management v1");
    });

我在控制器内有以下代码:

    /// <summary>
    /// Returns tag list
    /// </summary>
    /// <returns></returns>
    /// <response code="200">Returns tag list</response>
    [ProducesResponseType(typeof(List<TagDisplayAPI>), StatusCodes.Status200OK)]
    [HttpGet]
    [Route("")]
    [Authorize(Roles = "CompanyAdministrators")]
    public IActionResult GetAll()
    {
        var items = _mapper.Map<List<TagDisplayAPI>>(_service.GetAllTags(GetCarrierIdFromClaims()));
        return Ok(items);
    }

    /// <summary>
    /// Returns tag list for selected carrier
    /// </summary>
    /// <returns></returns>
    /// <response code="200">Returns tag list for selected carrier</response>
    [ProducesResponseType(typeof(List<TagDisplayAPI>), StatusCodes.Status200OK)]
    [HttpGet]
    [Route("/api/carrier/{carrierId:int:min(1)}/[controller]")]
    [Authorize(Roles = "SuperAdministrators")]
    public IActionResult GetAll(int carrierId)
    {
        var items = _mapper.Map<List<TagDisplayAPI>>(_service.GetAllTags(carrierId));
        return Ok(items);
    }

因此,正如我们所看到的,只有当用户具有角色CompanyAdministrators时,才能执行第一种方法,但是只有当用户具有角色SuperAdministrators时,才可以执行第二种方法。怎么说说才能在结果文档文件中显示此信息?谢谢

0 个答案:

没有答案