使用 Identity Server 在 .Net Core Web API 中授权
Authorization in .Net Core Web API using Identity Server
我们有一个 .Net Core Web API 托管在 Azure App Service 上,我们有自己的身份服务器来生成访问令牌。我们已将 Web API 添加到 Azure API 管理,因此所有对 Web API 的请求都必须通过 API 管理。
为了保护 API 操作,我们将在所有请求中发送由 Identity Server 生成的访问令牌。所以我们想验证访问令牌,我们在 API 管理中添加了身份验证 API 操作入站处理如下所示,因为我们不希望在 API 停止未经授权的请求不打后端的管理 API.
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url="https://<our-identity-server-URL>/.well-known/openid-configuration" />
</validate-jwt>
工作正常,所有没有访问令牌的请求都抛出 401 未经授权的响应,所有具有有效访问令牌的请求都在访问 Web API。
现在我们要通过检查访问令牌中是否存在所需的范围来授权我们的请求。但是因为我们已经在 API 管理中进行了身份验证,所以我们不想在 Web API 代码中再次进行身份验证。所以我们只在 StartUp.cs 文件中写了授权代码,如下所示,
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication("Bearer").AddJwtBearer("Bearer", config =>
{
config.Authority = "<our Identity Server URL>";
config.TokenValidationParameters = new TokenValidationParameters()
{
ValidateAudience = false
};
});
services.AddAuthorization(options =>
{
options.AddPolicy("ApiScope", policy =>
{
policy.RequireClaim("scope","Allow_BackEnd_Service");
});
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
//app.UseAuthentication(); (It is working only if I add this line)
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
但是这段代码总是返回 401 Unauthorized Result。如果我添加“app.UseAuthentication();”在 Startup.cs class 中的配置方法中,它正在运行。但是我们不想在 Web API.
中再次添加身份验证
是否可以只在.net Core Web 中进行授权API?
如果不是,如果我必须添加“app.UseAuthentication();”,延迟会是多少?会不会是性能问题?
此时可以直接使用APIM策略中的required-claims
来验证token中的scope
。
样本:
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url="https://<our-identity-server-URL>/.well-known/openid-configuration" />
<required-claims>
<claim name="scp" match="any" separator=" ">
<value>Users.Read</value>
</claim>
</required-claims>
</validate-jwt>
然后如果我的解码令牌中的范围如下所示,该策略将检查 Users.Read
是否在范围内,如果存在,则令牌有效。
我们有一个 .Net Core Web API 托管在 Azure App Service 上,我们有自己的身份服务器来生成访问令牌。我们已将 Web API 添加到 Azure API 管理,因此所有对 Web API 的请求都必须通过 API 管理。
为了保护 API 操作,我们将在所有请求中发送由 Identity Server 生成的访问令牌。所以我们想验证访问令牌,我们在 API 管理中添加了身份验证 API 操作入站处理如下所示,因为我们不希望在 API 停止未经授权的请求不打后端的管理 API.
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url="https://<our-identity-server-URL>/.well-known/openid-configuration" />
</validate-jwt>
工作正常,所有没有访问令牌的请求都抛出 401 未经授权的响应,所有具有有效访问令牌的请求都在访问 Web API。
现在我们要通过检查访问令牌中是否存在所需的范围来授权我们的请求。但是因为我们已经在 API 管理中进行了身份验证,所以我们不想在 Web API 代码中再次进行身份验证。所以我们只在 StartUp.cs 文件中写了授权代码,如下所示,
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication("Bearer").AddJwtBearer("Bearer", config =>
{
config.Authority = "<our Identity Server URL>";
config.TokenValidationParameters = new TokenValidationParameters()
{
ValidateAudience = false
};
});
services.AddAuthorization(options =>
{
options.AddPolicy("ApiScope", policy =>
{
policy.RequireClaim("scope","Allow_BackEnd_Service");
});
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
//app.UseAuthentication(); (It is working only if I add this line)
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
但是这段代码总是返回 401 Unauthorized Result。如果我添加“app.UseAuthentication();”在 Startup.cs class 中的配置方法中,它正在运行。但是我们不想在 Web API.
中再次添加身份验证是否可以只在.net Core Web 中进行授权API? 如果不是,如果我必须添加“app.UseAuthentication();”,延迟会是多少?会不会是性能问题?
此时可以直接使用APIM策略中的required-claims
来验证token中的scope
。
样本:
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url="https://<our-identity-server-URL>/.well-known/openid-configuration" />
<required-claims>
<claim name="scp" match="any" separator=" ">
<value>Users.Read</value>
</claim>
</required-claims>
</validate-jwt>
然后如果我的解码令牌中的范围如下所示,该策略将检查 Users.Read
是否在范围内,如果存在,则令牌有效。