【发布时间】:2019-05-07 17:05:06
【问题描述】:
ConfigurationDbContext 在生成令牌之前被释放。使用 IdentityServerTools 时。我已经尝试过手动添加 ConfigurationDbContext 和依赖配置存储中的配置
在我的 Startup.cs 中,我已经像这样配置了 IdentityServer:
//I've also tried without this line
services.AddDbContext<ConfigurationDbContext>(options
=> options.UseSqlServer(connectionString));
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddAspNetIdentity<ApplicationUser>()
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
s => s.MigrationsAssembly(migrationsAssembly));
// this enables automatic token cleanup. this is optional.
options.EnableTokenCleanup = true;
options.TokenCleanupInterval = 30;
})
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
s => s.MigrationsAssembly(migrationsAssembly));
});
我正在尝试从我的 api 生成一个令牌以将身份服务器用作客户端。有时 ConfigurationDbContext 已经被释放,有时它会在第一行抛出,有时它会在最后一行抛出
private async Task<string> CreatePaymentsTokenAsync()
{
// Get client for JWT
var idpClient = await this._configurationDbContext.Clients.Include(x => x.AllowedScopes)
.FirstOrDefaultAsync(c => c.ClientId == Config.APIServerClientId);
// Get scopes to set in JWT
var scopes = idpClient.AllowedScopes.Select(s => s.Scope).ToArray();
// Use in-built Identity Server tools to issue JWT
var token = await _identityServerTools.IssueClientJwtAsync(idpClient.ClientId,
idpClient.AccessTokenLifetime, scopes, new[] { "MyApi" });
return token;
}
这段代码偶尔会起作用,但大多数时候它会抛出上下文被释放的错误,有什么想法吗?
【问题讨论】:
标签: c# asp.net-web-api .net-core identityserver4