如果存在 UseIdentityServer,是否需要 AddAuthentication 和 AddAuthorization?
Is either AddAuthentication and AddAuthorization required if UseIdentityServer is present?
根据MSDN,我们有
The authentication middleware that is responsible for validating the request credentials and setting the user on the request context app.UseAuthentication()
The IdentityServer middleware that exposes the OpenID Connect endpoints app.UseIdentityServer()
然而,检查后一个方法声明,我看到以下内容,在我看来没有必要调用前者。
public static IApplicationBuilder UseIdentityServer(
this IApplicationBuilder app,
IdentityServerMiddlewareOptions options = null)
{
app.Validate();
app.UseMiddleware<BaseUrlMiddleware>();
app.ConfigureCors();
if (options == null) options = new IdentityServerMiddlewareOptions();
options.AuthenticationMiddleware(app);
app.UseMiddleware<MutualTlsEndpointMiddleware>();
app.UseMiddleware<IdentityServerMiddleware>();
return app;
}
不过,我对 UseAuthorization
一无所知。因此,我的结论是 here 提出的一般方法(即首先进行身份验证,然后进行授权),在我的案例中可以重新表述为以下内容。虽然我可以(应该?)跳过第二行,但我不能删除第三行(因为我在项目中使用了 [Authorize]
属性)。
builder.UseIdentityServer();
//builder.UseAuthentication();
builder.UseAuthorization();
对吗?
您不需要 UseAuthentication()
部分,因为正如您在问题中提到的那样,UseIdentityServer()
已经在幕后添加了该部分。
但您需要保留 UseAuthorization()
部分。
根据MSDN,我们有
The authentication middleware that is responsible for validating the request credentials and setting the user on the request context
app.UseAuthentication()
The IdentityServer middleware that exposes the OpenID Connect endpointsapp.UseIdentityServer()
然而,检查后一个方法声明,我看到以下内容,在我看来没有必要调用前者。
public static IApplicationBuilder UseIdentityServer(
this IApplicationBuilder app,
IdentityServerMiddlewareOptions options = null)
{
app.Validate();
app.UseMiddleware<BaseUrlMiddleware>();
app.ConfigureCors();
if (options == null) options = new IdentityServerMiddlewareOptions();
options.AuthenticationMiddleware(app);
app.UseMiddleware<MutualTlsEndpointMiddleware>();
app.UseMiddleware<IdentityServerMiddleware>();
return app;
}
不过,我对 UseAuthorization
一无所知。因此,我的结论是 here 提出的一般方法(即首先进行身份验证,然后进行授权),在我的案例中可以重新表述为以下内容。虽然我可以(应该?)跳过第二行,但我不能删除第三行(因为我在项目中使用了 [Authorize]
属性)。
builder.UseIdentityServer();
//builder.UseAuthentication();
builder.UseAuthorization();
对吗?
您不需要 UseAuthentication()
部分,因为正如您在问题中提到的那样,UseIdentityServer()
已经在幕后添加了该部分。
但您需要保留 UseAuthorization()
部分。