【发布时间】:2017-05-13 19:37:23
【问题描述】:
我正在开发一个身份验证服务器来传递令牌,以使用 IdentityServer4 来使用我们的 API。
我使用 MongoDB 作为允许用户获取令牌的数据库,为了更安全,我使用自定义证书来加密令牌。
这就是我的AuthenticationServer Startup.cs 的样子:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
var cert = new X509Certificate2(Path.Combine(_environment.ContentRootPath, "cert", "whatever.pfx"), "whatever");
services.AddIdentityServer().AddSigningCredential(cert)
.AddInMemoryApiResources(Config.Config.GetApiResources());
services.AddTransient<IUserRepository, UserRepository>();
services.AddTransient<IClientStore, ClientStore>();
services.AddTransient<IProfileService, UserProfileService>();
services.AddTransient<IResourceOwnerPasswordValidator, UserResourceOwnerPasswordValidator>();
services.AddTransient<IPasswordHasher<User.Model.User>, PasswordHasher<User.Model.User>>();
}
如您所见,我对那些执行客户端身份验证和密码验证的接口进行了自定义实现。这工作正常。
然后我用生成的令牌保护另一个应用程序,我在那里定义它必须使用IdentityServerAuthetication(localhost:5020 是我的AuthenticationServer 运行的地方)
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseCors("CorsPolicy");
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = "http://localhost:5020",
RequireHttpsMetadata = false,
ApiName = "MyAPI",
RoleClaimType = JwtClaimTypes.Role
});
app.UseMvc();
}
一切正常,但如果我关闭 AuthenticationServer,我会从我正在保护的 API 中收到此错误:
System.InvalidOperationException:IDX10803:无法从“http://localhost:5020/.well-known/openid-configuration”获取配置。 在 Microsoft.IdentityModel.Protocols.ConfigurationManager`1.d__24.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.d__1.MoveNext() 失败:Microsoft.AspNetCore.Server.Kestrel[13]
所以看起来 API 将前往 discovery 端点,以查看解密令牌的端点在哪里(我猜应该是 userinfo_endpoint)。
我的观点是:
- 似乎发现端点用于获取有关如何使用身份验证服务器的信息(对我来说,开放 API 是有意义的),但在我们的例子中,我们没有开发和开放 API,所以我们的客户端只是我们有协议的,我们会提前告诉他们端点,我们很可能会通过 IP 地址进行限制。
- 有什么方法可以停用发现端点并在 API 上设置证书以正确解密令牌。
也许我错过了完整的画面并且我在说一些愚蠢的事情,但我很乐意理解背后的概念。 提前致谢
【问题讨论】:
标签: c# mongodb openid identityserver4