【发布时间】:2020-06-15 19:28:22
【问题描述】:
所以我一直在尝试设置 IdentityServer4,使用了 PluralSight 的几个指南和公共可用指南。
我能够在 Core2.1 上运行一切,但是一旦我尝试在 Core3.1 上构建 IdentityServer4(所有最新版本的框架和与 Core2.1 示例完全相同的配置),即使存在令牌,我的 webAPI 也无法授权客户端应用程序。
WebAPI 是一个 .NET4.x 项目,使用 IdentityServer3.Contrib.AccessTokenValidation 包。
IDP 启动配置(Core2.1 和 Core3.1 项目相同):
services
.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddTestUsers(SeedData.GetUsers())
.AddInMemoryIdentityResources(SeedData.GetIdentityResources())
.AddInMemoryApiResources(SeedData.GetApiResources())
.AddInMemoryClients(SeedData.GetClients());
WebAPI 启动配置 (IdentityServer3.Contrib.AccessTokenValidation) 适用于 Core2.1 IdentityServer4,但不适用于 Core3.1 IdentityServer4
public void Configuration(IAppBuilder app) {
app.UseIdentityServerBearerTokenAuthentication(new IdentityServer3.AccessTokenValidation.IdentityServerBearerTokenAuthenticationOptions() {
Authority = "https://localhost:44314/",
ClientId = "kpcwebapi",
RequiredScopes = new[] { "kpcwebapi" },
});
}
客户端应用程序配置 适用于 Core2.1 IdentityServer4,但不适用于 Core3.1 IdentityServer4
services.AddOpenIdConnect("oidc", options => {
options.SignInScheme = "Cookies";
options.Authority = "https://localhost:44314/";
options.ClientId = "testHybrid";
options.ResponseType = "code id_token";
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("kpcwebapi");
options.SaveTokens = true;
options.ClientSecret = "secret";
options.GetClaimsFromUserInfoEndpoint = true;
options.ClaimActions.Remove("amr");
options.ClaimActions.DeleteClaim("sid");
options.ClaimActions.DeleteClaim("idp");
options.TokenValidationParameters = new TokenValidationParameters {
NameClaimType = JwtClaimTypes.GivenName,
RoleClaimType = JwtClaimTypes.Role,
};
});
我确定配置是正确的,因为在 Core2.1 上一切正常 我的问题,因为我自己找不到,Core3.1 IdentityServer4 有什么改变我的 WebAPI 不再工作了吗?我开始认为 IdentityServer4 的最新版本(针对 Core3.1)存在重大错误/问题……因为我们使用的是 OpenIDConnect……更新您的 IdentityServer 时授权应该仍然有效吗?
有趣的是,当 API 启动时,只要 IDP 没有运行,我就会得到一个异常,如果它正在运行,它会通过语句而没有错误/故障。 所以我的 API 确实建立了与 IDP(Core2.1 和 Core3.1)的连接,但是当它是 Core3.1 IDP 时,来自客户端应用程序的调用未经过身份验证/授权。
如果有人知道我做错了什么,我将非常感谢分享它,因为我刚刚浪费了 2 周的时间试图让它在 Core3.1 上运行......
【问题讨论】:
标签: oauth-2.0 identityserver4 openid-connect webapi