asp.net-mvc - IdentityServer4 和 Web API 在同一个项目中
问题描述
我有一个IdentityServer和一个MVC-Client,IdentityServer 有自己的 Web API 来为其客户端提供用户管理。MVC 客户端使用HybridAndClientCredentials
授权类型与 IdentityServer 进行交互。我对验证客户的用户没有任何问题。问题是当我尝试使用经过身份验证的用户从 IdentityServer 调用一些 API 时,服务器返回登录视图而不是 API 的结果。
这是我的客户端配置:
new Client
{
ClientId = "mvc.identity.management",
ClientName = "Identity Management",
AllowedGrantTypes = GrantTypes.Hybrid,
RequireConsent = false,
ClientSecrets =
{
new Secret("somesecret".Sha256())
},
RedirectUris = { "http://localhost:5001/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:5001/signout-callback-oidc" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile
}
}
和客户
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ClientSecret = "somesecret";
options.ResponseType = "code id_token";
options.GetClaimsFromUserInfoEndpoint = true;
options.ClientId = "mvc.identity.management";
options.SaveTokens = true;
});
任何建议都会有所帮助。
解决方案
也许为时已晚,但其他人可能会提出这个问题。解决方案是LocalApi
使用IdentityServer4
. 您创建一个LocalApi
并使用自定义范围授权它,您的客户应添加该范围,即IdentityServerApi
. 完整资源可在此处获得:
https ://docs.identityserver.io/en/latest/topics/add_apis.html
推荐阅读
- node.js - 如何将此代码生成的数据导出到 puppeteer 中的 CSV 文件?
- azure - Azure 函数返回 500 且没有错误
- php - phpseclib put 返回 true 但文件未上传
- gcc - `conda build gcc-5` 在 Mac OS Mojave 上失败
- ethereum - Solidity 是否支持浮点数
- c++ - 为什么模板和模板特化实体和类不是?
- vb.net - 实体外键不在模型中
- python - sphinx - 如何包含 Python 函数作为源代码
- javascript - 如何使用 Vue.js 将 TypeScript 文件编译为 JavaScript
- java - 为什么 Java Stream 没有得到 List<> 不能转换为布尔值