标签:identityserver4 asp-net-core asp-net-identity c
解决了我将UserManager添加到不会初始化IdentityServer4的API(User creation with IdentityServer4 from multiple API’s)的最初问题之后(该问题又在仅负责用户注册和登录的Web应用程序中初始化),我遇到了另一个问题.从同一API,我希望也可以从IdentityServer4的IConfigurationDbContext获取客户端和资源.
到目前为止,我正在尝试以下操作:
我在API的启动中添加ConfigurationDbContext,然后通过ClientsController和ClientsRepository尝试访问客户端,如下所示.
启动文件
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(defaultConnection, db => db.MigrationsAssembly("XXXXXXX"))
);
services.AddDbContext<ConfigurationDbContext>(options =>
options.UseNpgsql(defaultConnection, db => db.MigrationsAssembly("XXXXXXXX"))
);
services.AddIdentityCore<ApplicationUser>(options => {
options.Password.RequireNonAlphanumeric = false;
});
new IdentityBuilder(typeof(ApplicationUser), typeof(IdentityRole), services)
.AddRoleManager<RoleManager<IdentityRole>>()
.AddSignInManager<SignInManager<ApplicationUser>>()
.AddEntityFrameworkStores<ApplicationDbContext>();
ClientsRepository.cs(在.DataAccess中):
private readonly IConfigurationDbContext _context;
public bool AutoSaveChanges { get; set; } = true;
public ClientRepository(IConfigurationDbContext context)
{
_context = context;
}
public Task<Client> GetClientAsync(int id)
{
return _context.Clients
.Include(x => x.AllowedGrantTypes)
.Include(x => x.RedirectUris)
.Include(x => x.PostLogoutRedirectUris)
.Include(x => x.AllowedScopes)
.Include(x => x.ClientSecrets)
.Include(x => x.Claims)
.Include(x => x.IdentityProviderRestrictions)
.Include(x => x.AllowedCorsOrigins)
.Include(x => x.Properties)
.Where(x => x.Id == id)
.SingleOrDefaultAsync();
}
但是,我收到以下错误:
System.InvalidOperationException: Unable to resolve service for type 'IdentityServer4.EntityFramework.Interfaces.IConfigurationDbContext' while attempting to activate 'XXXXXX.Data.Repositories.ClientRepository'.
我猜想它又必须与服务启动做些事情,但是我似乎找不到它.
有人解决过类似的问题吗?
最好,
马里奥斯
解决方法:
如果要使用这些,只需使用IDS4.EFCore软件包中已经存在的ServicesCollection扩展:
services.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder => builder.UseNpgsql(defaultConnection, db => db.MigrationsAssembly("XXXXXXXX"));
});
这将为您添加IConfigurationDbContext,以便以后可以通过DI在其他服务中使用它.
如果要将其直接添加到ServicesCollection,请使用以下静态方法:
public static IServiceCollection AddConfigurationStore(this IServiceCollection services,
Action<ConfigurationStoreOptions> storeOptionsAction = null)
{
return services.AddConfigurationStore<ConfigurationDbContext>(storeOptionsAction);
}
public static IServiceCollection AddConfigurationStore<TContext>(this IServiceCollection services,
Action<ConfigurationStoreOptions> storeOptionsAction = null)
where TContext : DbContext, IConfigurationDbContext
{
var options = new ConfigurationStoreOptions();
services.AddSingleton(options);
storeOptionsAction?.Invoke(options);
if (options.ResolveDbContextOptions != null)
{
services.AddDbContext<TContext>(options.ResolveDbContextOptions);
}
else
{
services.AddDbContext<TContext>(dbCtxBuilder =>
{
options.ConfigureDbContext?.Invoke(dbCtxBuilder);
});
}
services.AddScoped<IConfigurationDbContext, TContext>();
return services;
}
标签:identityserver4,asp-net-core,asp-net-identity,c 来源: https://codeday.me/bug/20191108/2006645.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。