asp.net - 带有自定义 ClientStore 的 IdentityServer
问题描述
我有以下。它在测试环境中工作:
启动.cs:
public void ConfigureServices(IServiceCollection services) {
// non-IdentityServer code here
services.AddIdentityServer(
options => {
}
)
.AddApiAuthorization<UserEntity, LeagoDbContext>()
.AddClientStore<MyClientStore>()
;
services.AddAuthentication()
.AddIdentityServerJwt();
// more non-IdentityServer code
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
// non-IdentityServer code here
app.UseAuthentication();
app.UseIdentityServer();
// more non-IdentityServer code
}
MyClientStore.cs:
public async Task<Client> FindClientByIdAsync(string clientId) {
switch (clientId) {
case "client": // test store
return await Task.FromResult(MyTestClient.TestClient);
default:
// this is the problem. Not sure what to do.
}
如果我不在 Startup.cs 中添加 MyClientStore,则真实环境有效,但测试环境无效。所以我不知何故需要看看真正的客户是什么样子(这样我就可以重建它)。或者,我可以将默认客户端存储注入我的自定义客户端,并在上面 switch 语句的默认情况下调用它。但我不确定如何做任何一个。请注意,默认存储又依赖于 ILogger 和 IConfigurationDbContext。
解决方案
如果没有找到用户或根据您的自定义条件找到任何用户,您可以返回 null
public async Task<Client> FindClientByIdAsync(string clientId) {
switch (clientId) {
case "client": // test store
return await Task.FromResult(MyTestClient.TestClient);
default:
// you could return null here instead if no user
return await Task.FromResult(MyTestClient.AnotherClient);
}
推荐阅读
- sonarqube - 在 sonarqube 中添加 java 规则
- amazon-s3 - 从 avro 模式创建配置单元表时出错
- php - Laravel Ajax Get and Post request using controller
- java - 当代码意味着输入将始终为浮点数时,这是否会起作用?while 到底做了什么?
- android - Android popBackStackImmediate 无法移除 Glide 的 SupportRequestManagerFragment
- python - Python如何在一行中分配多个变量?
- python - 无法理解为什么两个程序没有返回相同的结果
- python - 格式化与Python2和Python3兼容的python异常消息的正确方法是什么?
- azure - 天蓝色存储VM数据盘添加
- oauth - VSTS,创建构建定义得到 AllowScriptsAuthAccess 错误