---
# System prepended metadata

title: IServiceCollection 未包含 AddAuthorization 的定義

---

# IServiceCollection 未包含 AddAuthorization 的定義

## 問題
想要照網路上的參考資料使用 AddAuthorization 的時候發現

``` C#=
 public override void ConfigureServices(ServiceConfigurationContext context)
 {
     Configure<AbpAutoMapperOptions>(options =>
     {
         options.AddMaps<BookStoreApplicationModule>();
     });
     var services = context.Services;

     services.AddAuthorization(options =>
     {
         options.AddPolicy("AdminOnly", policy =>
             policy.RequireClaim("Role", "Admin"));
     });
 }
```

## 原因
在ASP.NET Core 3.0版本中，AddAuthorization 方法已從 Microsoft.AspNetCore.Authorization 移到 Microsoft.AspNetCore.Authorization.Policy。
[更新資訊](https://learn.microsoft.com/en-us/dotnet/core/compatibility/aspnetcore#authorization-addauthorization-overload-moved-to-different-assembly)

## 解決方法
這意味著，如果要使用 AddAuthorization，需要引用 Microsoft.AspNetCore.Authorization.Policy。
另一個選擇是使用 AddAuthorizationCore 代替 AddAuthorization，它仍然位於 Microsoft.AspNetCore.Authorization。


改成下方：
``` C#=
 public override void ConfigureServices(ServiceConfigurationContext context)
 {
     Configure<AbpAutoMapperOptions>(options =>
     {
         options.AddMaps<BookStoreApplicationModule>();
     });
     var services = context.Services;

     services.AddAuthorizationCore(options =>
     {
         options.AddPolicy("AdminOnly", policy =>
             policy.RequireClaim("Role", "Admin"));
     });
 }
```