c# - 身份服务器承载未经过身份验证
问题描述
我有一个运行 Identity Server 4 的 dockerised .NET Core 项目,该项目可以正确地创建帐户、发出令牌并在其自己的容器中验证请求。我将其称为我的 AuthAPI
我的问题是当我尝试使用我的 AuthAPI 验证其他 API 上的令牌时
我有一个 API,我会调用我的 CustomerAPI。我正在尝试使用 AuthAPI 上的身份服务器来授权我的端点,但到目前为止我还没有运气
我测试此功能的方式是通过以下步骤:
- 在 AuthAPI 上创建一个帐户
- 确认 AuthAPI 帐户电子邮件
- 在 AuthAPI 上进行身份验证以检索我的承载令牌
- 向 AuthAPI 发出请求以获取要在我的 CustomerAPI 端点路由中使用的帐户 ID
- 将此 Bearer 令牌放入 Authorization Header 并使用 Authorize 属性向 CustomerAPI 端点发出请求
我使用我的 Bearer Token 对 AuthAPI 的授权调用验证并成功返回。我可以在我的 Identity Server 调试日志中看到以下短语:
AuthenticationScheme: Bearer was successfully authenticated.
当我尝试为我的 CustomerAPI 端点使用相同的承载令牌时,我得到以下调试输出:
AuthenticationScheme: Bearer was not authenticated.
我认为这是由于我的 CustomerAPI 配置失败,但尚未设法找到问题所在,这是我的配置:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(o =>
{
o.Authority = "http://authapi/";
o.Audience = "http://authapi/";
o.RequireHttpsMetadata = false;
});
任何帮助或指导将不胜感激,谢谢!
--- 编辑 --- 请求的配置文件:
public class Config
{
public static IEnumerable<IdentityResource> GetIdentityResource()
{
return new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Email(),
new IdentityResources.Profile()
};
}
public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("customerapi", "Customer API")
{
Scopes = {new Scope("api.read")}
}
};
}
public static IEnumerable<Client> GetClients()
{
return new []
{
new Client
{
RequireConsent = false,
ClientId = "demo_site",
ClientName = "Demo Site",
AllowedGrantTypes = GrantTypes.Implicit,
AllowedScopes = {"openid", "email", "profile", "api.read"},
RedirectUris = {"http://localhost:4200/auth-callback"},
PostLogoutRedirectUris = {"http://localhost:4200/"},
AllowedCorsOrigins = {"http://localhost:4200"},
AllowAccessTokensViaBrowser = true,
AccessTokenLifetime = 3600
}
};
}
}
解决方案
推荐阅读
- windows - Jenkins 上的调试模式 - 执行 Windows 批处理命令?
- arduino - 如何使用 MH-Z14a CO2 传感器?
- c - 使用 getopt optarg 的 fopen 错误问题
- power-automate - 在 Power Automate 中每月创建存储桶并在其中创建任务
- ms-word - 官员:在 .rmd 文件中插入横向格式的分节符
- spring - Springboot 后端无法连接来自 Vue 的 Axios 调用
- c# - WPF 打印问题:为什么我的 PageMediaSize 设置被忽略?
- python - 如何检查给定 DataFrame 列的每一行是否在指定范围内?
- python - 如何在类似 JSON 格式的文件中找到平均值?
- python - 如何通过简单的神经网络正确使用批处理 - 基本 Pytorch