gpt4 book ai didi

c# - IdentityServer 3 - 使用数据库中的客户端和范围

转载 作者:搜寻专家 更新时间:2023-10-30 19:52:46 27 4
gpt4 key购买 nike

我是 IdentityServer 3 的新手,示例和教程使用的是 InMemory 用户、客户端和范围,但我需要这些来自 DB。所以我做了:

启动.cs

public void Configuration(IAppBuilder app)
{
// Allow all origins
app.UseCors(CorsOptions.AllowAll);

var factory = new IdentityServerServiceFactory();

var userService = new UserService();
var clientStore = new ClientStore();
var scopeStore = new ScopeStore();
var corsService = new CorsService();

factory.UserService = new Registration<IUserService>(resolver => userService);
factory.ClientStore = new Registration<IClientStore>(resolver => clientStore);
factory.ScopeStore = new Registration<IScopeStore>(resolver => scopeStore);
factory.CorsPolicyService = new Registration<ICorsPolicyService>(resolver => corsService);



var options = new IdentityServerOptions
{
SiteName = "Embedded IdentityServer",
SigningCertificate = LoadCertificate(),
Factory = factory
};

app.UseIdentityServer(options);
}

但在 ClientStore 和 ScopeStore 中,我必须将我的客户端/范围数据库模型映射到 IdentityServer3.Core.Models 客户端/范围。像这样:

ClientStore.cs

public Task<Client> FindClientByIdAsync(string clientId)
{
var clientFromDb = _db.Clients.SingleOrDefault(x => x.ClientId == clientId);
var client = new Client
{
ClientName = clientFromDb.ClientName,
ClientId = clientFromDb.ClientId,
AccessTokenType = clientFromDb.AccessTokenType,
Enabled = clientFromDb.Enabled,
Flow = clientFromDb.Flow,
RedirectUris = clientFromDb.RedirectUris.Select(x => x.Uri).ToList(),
PostLogoutRedirectUris = clientFromDb.PostLogoutRedirectUris.Select(x => x.Uri).ToList(),
AllowedCorsOrigins = clientFromDb.AllowedCorsOrigins.Select(x => x.Origin).ToList(),
AllowedScopes = clientFromDb.AllowedScopes.Select(x => x.Scope).ToList(),
AllowAccessToAllScopes = clientFromDb.AllowAccessToAllScopes,
AccessTokenLifetime = clientFromDb.AccessTokenLifetime
};

return Task.FromResult(client);
}

知道我的数据库模型只是这些 IdentityServer3.Core.Models 的副本,有没有更好的方法来做到这一点?

最佳答案

您是否尝试过使用 AutoMapper >> https://automapper.org/

关于c# - IdentityServer 3 - 使用数据库中的客户端和范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41400086/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com