如何在我的 mongodb 数据库上实施 PersistedGrantStore
How can I implement PersistedGrantStore on my mongodb database
我正在尝试在 mongodb 上实施 PersistedGrantStore,我已经成功地使用 mongodb 来存储用户和客户端,现在我正在尝试存储授权而不是在内存授权存储中使用
我创建了一个继承自 IPersistedGrantStore
的 class
public class PersistedGrantStore : IPersistedGrantStore
{
public async Task<IEnumerable<PersistedGrant>> GetAllAsync(string subjectId)
{
PersistedGrantService persistedGrantService = new PersistedGrantService();
return await persistedGrantService.GetAllPersistedGrant(subjectId);
}
public async Task<PersistedGrant> GetAsync(string key)
{
PersistedGrantService persistedGrantService = new PersistedGrantService();
return await persistedGrantService.GetPersistedGrantByKey(key);
}
public async Task RemoveAllAsync(string subjectId, string clientId)
{
PersistedGrantService persistedGrantService = new PersistedGrantService();
await persistedGrantService.RemoveAllBySubjectIdAndClientId(subjectId, clientId);
}
public async Task RemoveAllAsync(string subjectId, string clientId, string type)
{
PersistedGrantService persistedGrantService = new PersistedGrantService();
await persistedGrantService.RemoveAllBySubjectIdAndClientIdAndType(subjectId, clientId, type);
}
public async Task RemoveAsync(string key)
{
PersistedGrantService persistedGrantService = new PersistedGrantService();
await persistedGrantService.RemoveAllByKey(key);
}
public async Task StoreAsync(PersistedGrant grant)
{
PersistedGrantService persistedGrantService = new PersistedGrantService();
await persistedGrantService.InsertPersistedGrant(grant);
}
}
并且在startup.cs
public void ConfigureServices(IServiceCollection services)
{
var builder = services.AddIdentityServer()
.AddSigningCredential(cert)
.AddInMemoryIdentityResources(IdentityConfiguration.GetIdentityResources())
.AddInMemoryApiResources(ApiResourceConfiguration.GetApiResources());
builder.Services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
builder.Services.AddTransient<IProfileService, ProfileService>();
builder.Services.AddTransient<IClientStore, ClientStore>();
builder.Services.AddTransient<IPersistedGrantStore, PersistedGrantStore>();
}
似乎无论我做什么 none PersistedGrantStore 中的函数都会被调用,我不确定我在这里遗漏了什么,但我仍然可以 ping 服务器并获取访问令牌,所以我假设内存存储是仍在使用中。
我似乎没有遇到应用程序需要存储使用 code/hybrid 流、引用令牌或提示同意的授权类型的情况。
当我向客户端添加 AllowOfflineAccess = true 时,我可以看到该集合是在数据库上创建的
https://github.com/IdentityServer/IdentityServer4/issues/699
我正在尝试在 mongodb 上实施 PersistedGrantStore,我已经成功地使用 mongodb 来存储用户和客户端,现在我正在尝试存储授权而不是在内存授权存储中使用 我创建了一个继承自 IPersistedGrantStore
的 classpublic class PersistedGrantStore : IPersistedGrantStore
{
public async Task<IEnumerable<PersistedGrant>> GetAllAsync(string subjectId)
{
PersistedGrantService persistedGrantService = new PersistedGrantService();
return await persistedGrantService.GetAllPersistedGrant(subjectId);
}
public async Task<PersistedGrant> GetAsync(string key)
{
PersistedGrantService persistedGrantService = new PersistedGrantService();
return await persistedGrantService.GetPersistedGrantByKey(key);
}
public async Task RemoveAllAsync(string subjectId, string clientId)
{
PersistedGrantService persistedGrantService = new PersistedGrantService();
await persistedGrantService.RemoveAllBySubjectIdAndClientId(subjectId, clientId);
}
public async Task RemoveAllAsync(string subjectId, string clientId, string type)
{
PersistedGrantService persistedGrantService = new PersistedGrantService();
await persistedGrantService.RemoveAllBySubjectIdAndClientIdAndType(subjectId, clientId, type);
}
public async Task RemoveAsync(string key)
{
PersistedGrantService persistedGrantService = new PersistedGrantService();
await persistedGrantService.RemoveAllByKey(key);
}
public async Task StoreAsync(PersistedGrant grant)
{
PersistedGrantService persistedGrantService = new PersistedGrantService();
await persistedGrantService.InsertPersistedGrant(grant);
}
}
并且在startup.cs
public void ConfigureServices(IServiceCollection services)
{
var builder = services.AddIdentityServer()
.AddSigningCredential(cert)
.AddInMemoryIdentityResources(IdentityConfiguration.GetIdentityResources())
.AddInMemoryApiResources(ApiResourceConfiguration.GetApiResources());
builder.Services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
builder.Services.AddTransient<IProfileService, ProfileService>();
builder.Services.AddTransient<IClientStore, ClientStore>();
builder.Services.AddTransient<IPersistedGrantStore, PersistedGrantStore>();
}
似乎无论我做什么 none PersistedGrantStore 中的函数都会被调用,我不确定我在这里遗漏了什么,但我仍然可以 ping 服务器并获取访问令牌,所以我假设内存存储是仍在使用中。
我似乎没有遇到应用程序需要存储使用 code/hybrid 流、引用令牌或提示同意的授权类型的情况。 当我向客户端添加 AllowOfflineAccess = true 时,我可以看到该集合是在数据库上创建的 https://github.com/IdentityServer/IdentityServer4/issues/699