如何使用 IdentityServer4 保护 API(免受意外调用)?
How to protect API (from unexpected calls) with IdentityServer4?
我有一个 3 层 Blazor 应用程序,并使用 IdentyServer 4.1 进行身份验证。
我目前的问题是某些 API 端点不受保护,任何应用程序都可以进行此类调用。
我的理解是,IdentityServer 只是经过身份验证的用户的服务器令牌,对吗?
有没有办法确保调用来自我的应用程序(网站、手机等...),
也许有令牌...但如何?
如果您使用 AddJwtBearer 处理程序保护您的 API,它将保护您的 API,并且只会接受您的 IdentityServer 颁发的令牌(比较 iss 声明)。
你也可以控制token的受众是你的API。这意味着您的 IdentityServer 可以颁发用于不同 API 的令牌(使用 aud 声明)。
只有使用IdentityServer 私钥签名的令牌才能被接受。该密钥的 public 部分由您的 API 在启动时自动下载。
所以,结论是安全的。
但这只是处理身份验证,然后在验证访问令牌后应用授权检查。
来自 mu 训练之一的图片 类 显示了管道的构建方式:
您需要使用 JwtBearer 身份验证来保护您的 Web-Api 端点。
这是您应该在 Web Api 的启动 class:
中的代码示例
Startup.ConfigureServices方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = "https://localhost:5000";
options.Audience = "weatherapi";
options.TokenValidationParameters = new TokenValidationParameters()
{
NameClaimType = "name"
};
});
Startup.Configure
// Code omitted...
app.UseCors(config =>
{
config.AllowAnyOrigin();
config.AllowAnyMethod();
config.AllowAnyHeader();
});
app.UseAuthentication();
app.UseAuthorization();
您还需要安装 Microsoft.AspNetCore.Authentication.JwtBearer 软件包,它添加了 ASP.NET 核心中间件,使应用程序能够接收 OpenID Connect 持有者令牌。
您还需要使用 Authorize
属性或您的控制器来注释您的受保护端点,如下所示:
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class WeatherForecastController : ControllerBase
{
}
注意:options.Authority = "https://localhost:5000";
是您的 IdentityServer 项目
请注意,options.Audience = "weatherapi";
在您的 IdentityServer 项目的 Config.cs 文件中设置如下:
public static IEnumerable<ApiResource> Apis =>
new ApiResource[]
{
new ApiResource("weatherapi", "The Weather API")
};
和
public static IEnumerable<Client> Clients =>
new Client[]
{
new Client
{
ClientId...,
AllowedScopes = { "openid", "profile", "email", "weatherapi" }
},
};
我有一个 3 层 Blazor 应用程序,并使用 IdentyServer 4.1 进行身份验证。
我目前的问题是某些 API 端点不受保护,任何应用程序都可以进行此类调用。
我的理解是,IdentityServer 只是经过身份验证的用户的服务器令牌,对吗? 有没有办法确保调用来自我的应用程序(网站、手机等...), 也许有令牌...但如何?
如果您使用 AddJwtBearer 处理程序保护您的 API,它将保护您的 API,并且只会接受您的 IdentityServer 颁发的令牌(比较 iss 声明)。
你也可以控制token的受众是你的API。这意味着您的 IdentityServer 可以颁发用于不同 API 的令牌(使用 aud 声明)。
只有使用IdentityServer 私钥签名的令牌才能被接受。该密钥的 public 部分由您的 API 在启动时自动下载。
所以,结论是安全的。
但这只是处理身份验证,然后在验证访问令牌后应用授权检查。
来自 mu 训练之一的图片 类 显示了管道的构建方式:
您需要使用 JwtBearer 身份验证来保护您的 Web-Api 端点。
这是您应该在 Web Api 的启动 class:
中的代码示例Startup.ConfigureServices方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = "https://localhost:5000";
options.Audience = "weatherapi";
options.TokenValidationParameters = new TokenValidationParameters()
{
NameClaimType = "name"
};
});
Startup.Configure
// Code omitted...
app.UseCors(config =>
{
config.AllowAnyOrigin();
config.AllowAnyMethod();
config.AllowAnyHeader();
});
app.UseAuthentication();
app.UseAuthorization();
您还需要安装 Microsoft.AspNetCore.Authentication.JwtBearer 软件包,它添加了 ASP.NET 核心中间件,使应用程序能够接收 OpenID Connect 持有者令牌。
您还需要使用 Authorize
属性或您的控制器来注释您的受保护端点,如下所示:
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class WeatherForecastController : ControllerBase
{
}
注意:options.Authority = "https://localhost:5000";
是您的 IdentityServer 项目
请注意,options.Audience = "weatherapi";
在您的 IdentityServer 项目的 Config.cs 文件中设置如下:
public static IEnumerable<ApiResource> Apis =>
new ApiResource[]
{
new ApiResource("weatherapi", "The Weather API")
};
和
public static IEnumerable<Client> Clients =>
new Client[]
{
new Client
{
ClientId...,
AllowedScopes = { "openid", "profile", "email", "weatherapi" }
},
};