asp.net-core - 客户端和用户使用 Identity Server 访问受保护的 API
问题描述
我想知道我希望在我的项目中使用的 Identity Server 的一个方面。
场景比较简单:
- 我有一个 API 和几个客户端
- 我想使用 Identity Server 和 OIDC 保护该 API
- 在一个客户端上,我正在研究具有可选身份验证的概念。
- 某些功能需要某些声明,即管理员
- Rest 应该允许匿名用户访问,但应该只允许来自注册客户端的请求
- 这也应该反映在 API 上
这里需要注意的是,我仍然希望将 API 限制为仅由 Identity Server 中的注册客户端访问
Identity Server 中是否可能出现这种情况?
我知道我会在 API 上设置管理策略,但我是否还必须设置 CORS 功能,或者 Identity Server 是否使用允许的范围和资源配置来执行此操作?
抱歉,如果解释不佳,我会尽快尝试添加图表
解决方案
是的,如果您希望仅将 API 访问限制为经过身份验证的请求,一种方法是通过设置全局授权策略来进行范围验证(请参阅https://github.com/IdentityServer/IdentityServer4.AccessTokenValidation)。
您的管理端点将检查其他声明。有几种方法可以做到这一点,一种方法是基于策略的授权 ( https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies?view=aspnetcore-2.2 )。
您将需要为您的 API 配置 CORS 中间件(请参阅https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-2.2)。如果您需要 COR 访问 Identity Server 端点,则需要配置 Identity Server(请参阅http://docs.identityserver.io/en/latest/topics/cors.html)。
推荐阅读
- windows - 每五帧添加关键帧?自动热键?
- perl - 为什么在 perl 中将 csv 转换为 xlsx 时文件大小会发生变化
- c# - C# 尽可能高效地从字符串中删除回车符、换行符和空格(基准)
- masstransit - DelegateEndpointDefinition IsTemporary
- javascript - Snackbar 未在页面中呈现
- reactjs - Material-Table里面的开关打不开
- ant - Jacoco 覆盖在 java 类中为 org/apache/log4j/Category 提供错误
- java - JPA 与共享主键的单向@OneToOne 关系始终触发辅助查询,即使 fetchType 为 EAGER
- flutter - 如何使用 bloc flutter 更改单个列表项的状态?
- python - TensorFlow 导入错误