首页 > 解决方案 > 如果存在 UseIdentityServer,是否需要 AddAuthentication 和 AddAuthorization?

问题描述

根据MSDN,我们有

负责验证请求凭据并在请求上下文中设置用户的身份验证app.UseAuthentication()
中间件 公开 OpenID Connect 端点的 IdentityServer 中间件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。所以我的结论是,这里介绍的一般方法(即首先验证,然后授权)在我的情况下可以重新表述为以下内容。虽然我可以(应该?)跳过第二行,但我不能删除第三行(因为我[Authorize]在项目中使用了 eg 属性)。

builder.UseIdentityServer();
//builder.UseAuthentication();
builder.UseAuthorization();

那是对的吗?

标签: c#identityserver4

解决方案


您不需要该UseAuthentication()部分,因为正如您在问题中提到的那样,它已经UseIdentityServer()在引擎盖下添加了。

但是你需要保留这UseAuthorization()部分。


推荐阅读