如何在我的mongodb数据库上实现PersistedGrantStore

时间:2017-01-15 06:49:02

标签: c# identityserver4 asp.net-core-1.1

我正在尝试在mongodb上实现PersistedGrantStore,我已成功使用mongodb来存储用户和客户端,现在我正在尝试存储授权而不是在内存授予存储中使用 我创建了一个继承自IPersistedGrantStore

的类
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>();
    }

似乎无论我做什么都没有调用PersistedGrantStore中的任何函数,我不知道我在这里丢失了什么,仍然可以ping服务器并获取访问令牌,所以我假设内存存储仍在使用。

1 个答案:

答案 0 :(得分:1)

似乎我没有遇到应用程序需要存储授权类型的情况,这些类型使用代码/混合流,引用令牌或提示同意。 当我将AllowOfflineAccess = true添加到客户端时,我可以看到在DB上创建了集合 https://github.com/IdentityServer/IdentityServer4/issues/699