Opendidictを使用して設定された認証/承認およびその他のドメイン関連機能のために、バックエンドマイクロサービス(コア2.0で開発された)と通信するモバイル(ネイティブ)およびウェブアプリケーション(SPA)があります。どちらのアプリもアクセストークンを取得しています。私が苦労しているのは、すべてのマイクロサービスは、ベアラアクセストークンと認証/認証(中央認証サービス)、認証マイクロサービス(OpenIddict 2. *)で生成されたアクセストークンを受け入れるべきです。では、REST APIに[Authorize]とマークされているマイクロサービスでは、何が変更されていないのですか?OpenIddict - マイクロサービスによる認証と認証
認証Microserviceからコード:
public void ConfigureServices(IServiceCollection services)
{
var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<IdentityDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
options.UseOpenIddict();
});
services.AddAuthentication().AddOAuthValidation();
services.AddOpenIddict(options =>
{
options.AddEntityFrameworkCoreStores<IdentityDbContext>();
options.AddMvcBinders();
options.EnableTokenEndpoint("/connect/token");
// Enable the password flow.
options.AllowPasswordFlow().AllowRefreshTokenFlow();
options.SetRefreshTokenLifetime(TimeSpan.FromHours(1));
options.DisableHttpsRequirement();
});
services.AddDbContext<AuthDbContext>(options => options.UseSqlServer(connection));
services.AddScoped<IUserRepository, UserRepository>();
services.AddAuthentication(options =>
{
options.DefaultScheme = OAuthValidationDefaults.AuthenticationScheme;
});
services.AddAuthorization(options =>
{
options.AddPolicy("RequireAdministratorRole", policy => policy.RequireRole("Administrator"));
});
}
通知Microservice内の既存のコード
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MastersDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddAuthentication().AddOAuthValidation();
services.AddAuthentication(options =>
{
options.DefaultScheme = OAuthValidationDefaults.AuthenticationScheme;
});
services.AddAuthorization(options =>
{
options.AddPolicy("RequireAdministratorRole", policy => policy.RequireRole("Administrator"));
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors(builder =>
builder.WithOrigins("*")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin()
);
//app.UseAntiforgeryToken();
app.UseMvc();
app.UseAuthentication();
}
通知制御:正しく、すべてのマイクロサービスによって解読されるトークンの
// POST api/values
[HttpPost]
[Authorize]
public IActionResult Post(Notification notification)
{
//logic
return Ok();
}
返信いただきありがとうございます。私の場合、マイクロサービスはDockerでホストされるため、共有フォルダを使用するのは難しいでしょう。 JwtBearerを使用して、着信トークンを認証するためにConfigureAuthServiceを使用するサンプルがあります。 AuthUrlとして設定されたAuthority値を使用して行われた認証のように見えます。 OpenIddictを使っても同じ結果が得られますか? – Satish
@Satish可能です。署名証明書を登録し、 'options.UseJsonWebTokens()'を呼び出すと、OpenIddictは強制的にJWTアクセストークンを発行します。次に、OAuth2検証ハンドラをJWTハンドラに置き換えてください。そうすればうまくいくはずです。 – Pinpoint
ConfigureAuthService { JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); var identityUrl = Configuration.GetValue( "AuthUrl"); services.AddAuthentication(オプション=> { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; })AddJwtBearer(オプション=> { options.Authority = identityUrl; options.RequireHttpsMetadata = FALSE;} )。 } –
Satish