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
Asp.net core 如何将Asp.net核心标识用户连接到IdentityServer4用户_Asp.net Core_Authorization_Asp.net Core Webapi_Identityserver4 - Fatal编程技术网

Asp.net core 如何将Asp.net核心标识用户连接到IdentityServer4用户

Asp.net core 如何将Asp.net核心标识用户连接到IdentityServer4用户,asp.net-core,authorization,asp.net-core-webapi,identityserver4,Asp.net Core,Authorization,Asp.net Core Webapi,Identityserver4,我有一个IdentityServer4,它部署了一个支持SQL server数据库。 我还部署了一个Asp.NET核心WebAppi服务,它使用IdentityServer4作为授权。 然后,我有一个Typescrip SPA客户端,它(目前)使用IdenityServer4登录并获取JWTToken以连接到WebAPI 如何在WebApi上使用Microsoft标识来管理授权。我的意思是,现在当我对IdentityServer进行身份验证时,我在我的WebApi控制器中得到了一个用户对象。此用

我有一个IdentityServer4,它部署了一个支持SQL server数据库。 我还部署了一个Asp.NET核心WebAppi服务,它使用IdentityServer4作为授权。 然后,我有一个Typescrip SPA客户端,它(目前)使用IdenityServer4登录并获取JWTToken以连接到WebAPI

如何在WebApi上使用Microsoft标识来管理授权。我的意思是,现在当我对IdentityServer进行身份验证时,我在我的WebApi控制器中得到了一个用户对象。此用户绑定到IdentityServer4,并拥有其声明。我想从我的WebApi将此用户连接到本地Asp.NEt身份用户,以便我可以管理它的角色并专门声明此WebApi


在WebApi上,我希望使用Microsoft asp.net标识在本地存储特定于该WebApi的用户、用户角色、用户声明,并且仅使用IdentityServer 4进行身份验证。如何将IS4用户映射到我的Asp.NEt核心标识用户?

尽管您可以从IdentityServer的UserInfo端点获取用户角色,并执行与Asp.NEt标识相同的操作

所以,在所有令牌的验证操作之后,您必须检查数据库中是否存在用户。如果不存在,则将用户添加到数据库中

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

        }).AddJwtBearer(options =>
        {
            options.Authority = "";
            options.RequireHttpsMetadata = false;
            options.Audience = "";
            options.Events.OnTokenValidated = async (context) =>
            {
                var usermanager = services.BuildServiceProvider().GetService<UserManager<ApplicationUser>>();
                var user = await usermanager.FindByNameAsync(context.HttpContext.User.Identity.Name);

                if (user == null)
                {
                    user = new ApplicationUser();
                    user.UserName = context.HttpContext.User.Identity.Name;
                    //...

                    await usermanager.CreateAsync(user);
                }
            };
        });
services.AddAuthentication(选项=>
{
options.DefaultAuthenticateScheme=JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme=JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(选项=>
{
选项。权限=”;
options.RequireHttpsMetadata=false;
选项。受众=”;
options.Events.OnTokenValidated=异步(上下文)=>
{
var usermanager=services.BuildServiceProvider().GetService();
var user=await usermanager.FindByNameAsync(context.HttpContext.user.Identity.Name);
if(user==null)
{
user=newapplicationuser();
user.UserName=context.HttpContext.user.Identity.Name;
//...
等待usermanager.CreateAsync(用户);
}
};
});

谢谢您的快速回复。这就是我要找的。