使用带有隐式流/授权类型的Identity Server 4,.NetCore2.0和MS Identity;
我不清楚以下各项的责任,因为每项具体涉及验证/授权持有人令牌。
我有以下启动:
public void ConfigureServices(IServiceCollection services) {
...
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = GetAuthentication().ApiURL;
options.RequireHttpsMetadata = false;
options.ApiName = "afapps";
});
// Below needed to inject UserManager<ApplicationUser> userManager
// elsewhere in app as this happens to be the authORization server
// as opposed to authENtication server.
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AuthDbContext>()
.AddDefaultTokenProviders();
}
public void Configure(IApplicationBuilder app) {
app.UseAuthentication();
app.UseMvc();
}
如果我在启动时省略services.AddIdentity<ApplicationUser, IdentityRole>()...
。在控制器中,我可以成功使用[授权],我的其他自定义ActionFilters
将HttpContext.User.Identity.IsAuthenticated
显示为== true
。
但是,添加services.AddIdentity<ApplicationUser, IdentityRole>()...
以启用Identity UserManager<ApplicationUser>
的使用后;我现在必须另外向每个控制器添加[Authorize(AuthenticationSchemes = "Bearer")]
..
有没有办法合并或安排services.AddAuthentication()
和services.AddIdentity()
,以便我不必指定[Authorize(AuthenticationSchemes = "Bearer")]
?
答案 0 :(得分:7)
在 AddAuthentication(Action<AuthenticationOptions> configureOptions)
之后使用AddIdentity()
覆盖来手动设置选项,如下所示:
services.AddAuthentication(options =>
{
options.DefaultScheme = "Bearer";
options.DefaultAuthenticateScheme = "Bearer";
options.DefaultChallengeScheme = "Bearer";
options.DefaultSignInScheme = "Bearer";
});
您必须执行此操作,因为字符串覆盖仅设置DefaultScheme
,而AddIdentity()
sets the more specific options。根据{{3}},DefaultScheme
仅用作所有其他人的后备广告。