APIの保護

IdentityServerはデフォルトで JWT (JSON Webトークン)形式のアクセストークンを発行します。

JWTのトークンを検証するための関連プラットフォームはすべて今日サポートされています。JWTライブラリの一覧は here からご覧いただけます。人気のあるライブラリは、例えば:

ASP.NET CoreベースのAPIを保護することは、DIでJWTベアラ認証ハンドラを構成し、認証ミドルウェアをパイプラインに追加することだけです。

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                // base-address of your identityserver
                options.Authority = "https://demo.identityserver.io";

                // name of the API resource
                options.Audience = "api1";
            });
    }

    public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
    {
        app.UseAuthentication();
        app.UseMvc();
    }
}

IdentityServer認証ハンドラ

私たちの認証ハンドラは上記のハンドラと同じ目的を果たします(実際にはMicrosoft JWTライブラリを内部的に使用します)が、いくつかの追加機能を追加しています:

  • JWTと参照トークンの両方のサポート
  • 参照トークンのための拡張可能なキャッシング
  • 統一された構成モデル
  • 有効範囲の検証

最も単純なケースでは、ハンドラの設定は上記のスニペットと非常に似ています:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(options =>
            {
                // base-address of your identityserver
                options.Authority = "https://demo.identityserver.io";

                // name of the API resource
                options.ApiName = "api1";
            });
    }

    public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
    {
        app.UseAuthentication();
        app.UseMvc();
    }
}

あなたは nuget または github からパッケージを取得することができます。

参照トークンのサポート

受信したトークンがJWTでない場合、ミドルウェアは検出文書にあるイントロスペクションエンドポイントに連絡してトークンを検証します。イントロスペクションエンドポイントでは認証が必要なため、設定済みのAPIシークレットを提供する必要があります。

.AddIdentityServerAuthentication(options =>
{
    // base-address of your identityserver
    options.Authority = "https://demo.identityserver.io";

    // name of the API resource
    options.ApiName = "api1";
    options.ApiSecret = "secret";
})

通常、着信要求ごとにイントロスペクションエンドポイントへのラウンドトリップを実行する必要はありません。ミドルウェアには、次のように有効にできるビルトインキャッシュがあります。

.AddIdentityServerAuthentication(options =>
{
    // base-address of your identityserver
    options.Authority = "https://demo.identityserver.io";

    // name of the API resource
    options.ApiName = "api1";
    options.ApiSecret = "secret";

    options.EnableCaching = true;
    options.CacheDuration = TimeSpan.FromMinutes(10); // that's the default
})

ハンドラは、DIコンテナに登録されている IDistributedCache 実装(たとえば、標準の MemoryDistributedCache )を使用します。

スコープの検証

ApiNameのプロパティをチェックトークンは、一致する視聴者(又はショートがある場合aud)請求項を。

IdentityServerでは、APIを複数のスコープに細分することもできます。細分性が必要な場合は、ASP.NETコア認可ポリシーシステムを使用してスコープを確認できます。

グローバルポリシーの作成:

services
    .AddMvcCore(options =>
    {
        // require scope1 or scope2
        var policy = ScopePolicy.Create("scope1", "scope2");
        options.Filters.Add(new AuthorizeFilter(policy));
    })
    .AddJsonFormatters()
    .AddAuthorization();

スコープポリシーの作成:

services.AddAuthorization(options =>
{
    options.AddPolicy("myPolicy", builder =>
    {
        // require scope1
        builder.RequireScope("scope1");
        // and require scope2 or scope3
        builder.RequireScope("scope2", "scope3");
    });
});