Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用IdentityServer4将网络身份添加到API_C#_Asp.net Core_Identityserver4 - Fatal编程技术网

C# 如何使用IdentityServer4将网络身份添加到API

C# 如何使用IdentityServer4将网络身份添加到API,c#,asp.net-core,identityserver4,C#,Asp.net Core,Identityserver4,我正在构建一个.net核心应用程序,我有IdentityServer4和一个运行正常的MVC客户端 在启动我的IdentityServer时,我可以成功添加Asp.Net标识,并且我可以访问本机UserManager和我的UserContext(它扩展了IdentityDbContext) 我还有一个API,我的MVC客户端(或其他客户端)可以调用它来获取有关登录用户的信息。此API成功地依赖于Identity Server,可以验证它接收的access_令牌,提取sud,打开UserConte

我正在构建一个.net核心应用程序,我有IdentityServer4和一个运行正常的MVC客户端

在启动我的IdentityServer时,我可以成功添加Asp.Net标识,并且我可以访问本机UserManager和我的UserContext(它扩展了IdentityDbContext)

我还有一个API,我的MVC客户端(或其他客户端)可以调用它来获取有关登录用户的信息。此API成功地依赖于Identity Server,可以验证它接收的access_令牌,提取sud,打开UserContext(在公共程序集中定义),找到用户并读取我添加到架构中的任何自定义表。不幸的是,我无法让API注册Asp.Net标识,因此无法使用UserManager查看角色和其他本机表

以下是IdentityServer启动:

public void ConfigureServices(IServiceCollection services)
{

    services.AddIdentity<ApplicationUser, ApplicationRole>()
    .AddEntityFrameworkStores<UserContext>()
    .AddDefaultTokenProviders();

    services.AddMvc();

    services.AddIdentityServer()
    .AddDeveloperSigningCredential()
    .AddAspNetIdentity<ApplicationUser>()
    .AddConfigurationStore(options =>
    {
        options.ConfigureDbContext = builder =>
        builder.UseSqlServer(IdentityConnectionString,
            sql => sql.MigrationsAssembly(migrationsAssembly));
    })
    .AddOperationalStore(options =>
    {
        options.ConfigureDbContext = builder =>
        builder.UseSqlServer(IdentityConnectionString,
            sql => sql.MigrationsAssembly(migrationsAssembly));
    });
}
public void配置服务(IServiceCollection服务)
{
服务附加性

信息:Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] 已成功验证令牌

信息:Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[8] AuthenticationScheme:BealerIdentityServerAuthenticationJWT已成功通过身份验证

信息:IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationHandler[8] AuthenticationScheme:承载已成功通过身份验证

信息:Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] 用户Joe Bloke的授权已成功

如果在API启动时添加Asp.Net标识,如下所示:

services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<UserContext>()
.AddDefaultTokenProviders();
services.AddIdentity

信息:Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2] 用户的授权失败:(null)

信息:Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3] 在筛选器“Microsoft.AspNetCore.Mvc.Authorization.authorizationFilter”处对请求的授权失败

这将导致重定向到API上不存在的登录页面,从而导致404。 我已尝试在API中添加对.addAsNetIdentity()的调用,但在AddIdentityServerAuthentication之后似乎无法识别该调用

是否可以让API访问Asp.Net Identity UserManager? 我需要做什么来修复此身份验证错误

谢谢

尝试使用此代码

services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })

您是否在配置(IAApplicationBuilder应用程序)中使用app.UseAuthentication()方法。是的,我是。但我认为这是从IdentityServer 4服务获取绑定,而不是从我的asp.net服务获取绑定?@ndu如果我有一个类似的用例,您可以在其中找到解决方案吗?嗨,不,我没有。这个项目暂时搁置,但无论如何我的计划略有改变:IdentityServer可以直接访问AspIdentity UserManager,但不是API。相反,API将使用我添加的一些自定义表,我在其中手动管理它们的角色。这是模型的一部分,因此任何有权访问模型的项目都可以看到这些表
services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })