---
# System prepended metadata

title: 全てのWeb APIに デフォルトのAuthorize属性を付与する方法 Roles 2023-07-24
tags: [Web API, Microsoft ID, Bearer, C#, Authorize, ASP.NET Core]

---

---
lang: ja-jp
breaks: true
---
# 参考資料

> Apply [Authorize] attribute implicitly to all Web API controllers
> https://stackoverflow.com/questions/21916870/apply-authorize-attribute-implicitly-to-all-web-api-controllers

> [Authorize]を使わなくても有効にする方法
> https://qiita.com/mkuwan/items/bd5ff882108998d76dca


## 一括でロールを設定する

Program.cs
```csharp=
builder.Services.AddControllers(options =>
{
    var policy = new AuthorizationPolicyBuilder()
        .RequireRole("API.Console")
        .Build();
    options.Filters.Add(new AuthorizeFilter(policy));
});
```



