小编Dav*_*is4的帖子

使用 IdentityServer4 从多个 API 创建用户

所以我一直在为这个问题痛心疾首。

我们有一个 Web 应用程序,它使用IdentityServer4AspNetIdentity来验证和注册用户(这是按预期工作的)。此外,我们还有其他 API(在同一解决方案中),可以使用 IdentityServer4 对访问 API 的用户进行身份验证

然而,问题是,除了身份验证,我们不能使用 API 来创建新用户

例如,用户应该能够通过 Web API 而不仅仅是从 Web 应用程序创建其他用户,因为在我们的例子中,用户链接到其他用户(将其视为多个配置文件)。

我不太熟悉 .Net Core 框架提供的所有配置服务,我尝试了多种通过 API 访问 Web 应用程序的用户管理器以通过经典 POST 请求注册我的用户的方法,但似乎没有任何效果. 在线搜索很棘手,因为我们的问题非常具体,这就是我在这里发帖的原因。

API Startup.cs - 配置服务:

services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
     // base-address of your identityserver
     options.Authority = Configuration["IdentityServer:Url"];

     // name of the API resource
     options.ApiName = Configuration["IdentityServer:APIName"];
     options.ApiSecret = Configuration["IdentityServer:APISecret"];

     options.EnableCaching = true;
     options.CacheDuration = TimeSpan.FromMinutes(10); // that's the default

     options.RequireHttpsMetadata = Convert.ToBoolean(Configuration["IdentityServer:RequireHttpsMetadata"]); …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-identity asp.net-core identityserver4

6
推荐指数
2
解决办法
5393
查看次数

通过不同API在IndetityServer4中使用ConfigurationDbContext

解决了我将UserManager添加到未初始化IdentityServer4 的API(通过多个API使用IdentityServer4创建用户)的最初问题之后(该问题又在仅负责用户注册和登录的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>();
Run Code Online (Sandbox Code Playgroud)

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 …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-identity asp.net-core identityserver4

3
推荐指数
1
解决办法
381
查看次数