问题描述
我是IdentityServer4的新手.我读到我需要实现一个IPersistedGrantStore
,以将刷新令牌存储到数据库中的PersistedGrants
之类的表中.
I'm new at IdentityServer4. I read I need to implement an IPersistedGrantStore
to store refresh tokens into a table like PersistedGrants
in my database.
IdentityServer日志如下:"refresh_token" grant with value: "{value}" not found in store
.
IdentityServer logs is the following when my native app ask for a new access token: "refresh_token" grant with value: "{value}" not found in store
.
那是因为我正在使用持久授权库的内存版本.因此,我需要将刷新令牌存储在PersistedGrant
表中.
That's because I'm using in-memory version of the persisted grant store. So I need to store refresh token in a PersistedGrant
table.
因此在我的 startup.cs 中,添加了以下行:
Thereforein my startup.cs I added the following line:
builder.Services.AddScoped<IPersistedGrantStore, PersistedGrantStore>();
和 IPersistedGrantStore.cs 是
public interface IPersistedGrantStore
{
Task StoreAsync(CustomPersistedGrant grant);
Task<CustomPersistedGrant> GetAsync(string key);
Task<IEnumerable<CustomPersistedGrant>> GetAllAsync(string subjectId);
}
所以我有一个 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; }
}
,现在我必须为 PersistedGrantStore.cs 类编写代码.但是问题是:一旦我为 PersistedGrantStore.cs 类编写代码,然后在其中调用PersistedGrantStore.cs
类?在Identity.Server Account/AccountController
中?在不使用EntityFramework的情况下,我找不到任何示例,因为我不想使用Entity Framework.
and now I have to write the code for my PersistedGrantStore.cs class.But the question is: once I have write code for PersistedGrantStore.cs class where I call PersistedGrantStore.cs
class? In Identity.Server Account/AccountController
? I didn't find any example about it without use EntityFramework, because I don't want to use Entity Framework.
谢谢.
推荐答案
关键是使用您喜欢的任何后端来实现IPersistedGrantStore
,然后通过在依赖项注入系统中注册该实现来告诉IdentityServer使用该实现.
The key will be to implement IPersistedGrantStore
using whatever backend you like, then to tell IdentityServer to use that implementation by registering the implementation in the dependency injection system.
例如,如果您调用实现PersistedGrantStore
,则可以这样注册实现:
For example, if you call your implementation PersistedGrantStore
, then you could register the implementation like this:
services.AddTransient<IPersistedGrantStore, PersistedGrantStore>();
您可以看到,基本上这就是 EntityFramework实现.
You can see that essentially this is all that the EntityFramework implementation does, once you take away all the EntityFramework stuff.
稍后,当IdentityServer想要保留授予时,它将获得您的实现并调用适当的方法.因此,除了将实现注入到IdentityServer中以执行所需的操作之外,您无需执行任何操作.
Later when IdentityServer wants to persist a grant, it will get your implementation and call the appropriate method. So you don't have to do anything, other than inject your implementation into IdentityServer so it can do whats needed.
这篇关于IdentityServer4-如何使用mysql.data将刷新令牌存储到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!