IdentityServer4-如何使用mysql.data将刷新令牌存储到数据库中?

Min*_*v 1 5 persistence identityserver4 refresh-token

我是IdentityServer4的新手。我读到我需要实现一个IPersistedGrantStore将刷新令牌存储到PersistedGrants数据库中的表中。

当本机应用程序请求新的访问令牌时,IdentityServer日志如下:"refresh_token" grant with value: "{value}" not found in store

那是因为我正在使用持久授权存储的内存版本。因此,我需要在PersistedGrant表中存储刷新令牌。

因此,在我的startup.cs中,添加了以下行:

builder.Services.AddScoped<IPersistedGrantStore, PersistedGrantStore>();
Run Code Online (Sandbox Code Playgroud)

IPersistedGrantStore.cs

public interface IPersistedGrantStore
{        
    Task StoreAsync(CustomPersistedGrant grant);

    Task<CustomPersistedGrant> GetAsync(string key);

    Task<IEnumerable<CustomPersistedGrant>> GetAllAsync(string subjectId);        
}
Run Code Online (Sandbox Code Playgroud)

所以我有一个CustomPersistedGrant.cs

public class CustomPersistedGrant
{
    public string Key { get; set; }

    public string Type { get; set; }

    public string SubjectId { get; set; }

    public string ClientId { get; set; }

    public DateTime CreationTime { get; set; }

    public DateTime? Expiration { get; set; }

    public string Data { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在我必须为PersistedGrantStore.cs类编写代码。但是问题是:一旦我为PersistedGrantStore.cs类编写了代码,我在哪里调用PersistedGrantStore.cs类?在Identity.Server中Account/AccountController?在不使用EntityFramework的情况下,我找不到任何示例,因为我不想使用Entity Framework。

谢谢。

Jim*_*nts 6

关键是IPersistedGrantStore使用您喜欢的任何后端来实现,然后通过在依赖项注入系统中注册实现来告诉 IdentityServer 使用该实现。

例如,如果您调用实现PersistedGrantStore,那么您可以像这样注册实现:

services.AddTransient<IPersistedGrantStore, PersistedGrantStore>();

您可以看到,一旦您去掉所有EntityFramework 内容,这基本上就是 EntityFramework 实现所做的全部工作。

稍后,当 IdentityServer 想要保留授予时,它将获取您的实现并调用适当的方法。因此,除了将您的实现注入到 IdentityServer 中以便它可以执行所需的操作之外,您无需执行任何操作。

  • 谢谢。在我的 **startup.cs** 中,我已使用以下行注册了实现: `builder.Services.AddScoped&lt;IPersistedGrantStore, PersistedGrantStore&gt;();` 。我的目标是保留授权,但是使用“IPersistedGrantStore”和 IdentityServer4 中的“PersistedGrantStore”保留授权的代码在哪里。我在 github 上的 QuickStartIdentityServer 项目中没有找到任何参考 (2认同)