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 .NET核心中的IdentityServer令牌授权_Asp.net Core_Identityserver3 - Fatal编程技术网

Asp.net core .NET核心中的IdentityServer令牌授权

Asp.net core .NET核心中的IdentityServer令牌授权,asp.net-core,identityserver3,Asp.net Core,Identityserver3,希望有人能帮上忙 我有一个IdentityServer3设置,我们正在现有项目中使用范围授权 我们刚刚在.NETCore上创建了一个API应用程序,遇到了一些问题,以及如何让它按预期工作 这个API资源实际上是一个中间人。在客户端上请求令牌,然后客户端又调用API。相当标准的型号 我们希望使用授权来处理两种情况: 1.令牌是有效的 2.令牌对于API资源的指定范围有效(例如[Authorize(scope=“Scope1”)] 然而,看起来API根本没有连接到IS3实例 代码如下所示: 启动 s

希望有人能帮上忙

我有一个IdentityServer3设置,我们正在现有项目中使用范围授权

我们刚刚在.NETCore上创建了一个API应用程序,遇到了一些问题,以及如何让它按预期工作

这个API资源实际上是一个中间人。在客户端上请求令牌,然后客户端又调用API。相当标准的型号

我们希望使用授权来处理两种情况: 1.令牌是有效的 2.令牌对于API资源的指定范围有效(例如[Authorize(scope=“Scope1”)]

然而,看起来API根本没有连接到IS3实例

代码如下所示:

启动

services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "http://identityserver3server";
                //options.ApiSecret = "";
                options.ApiName = "ApiName";
                options.LegacyAudienceValidation = true;
            });
options.ApiName-从我的阅读来看,这似乎与范围相关,但在这个级别上,我们不希望在这个级别的范围中指定要求,而是希望在控制器中这样做。 options.ApiSecret-这是必需的吗

services.AddAuthorization(options =>
        {
            options.AddPolicy("Scope1", policy => policy.Requirements.Add(new HasScopeRequirement("Scope1", "")));
        });
范围要求

 public class HasScopeRequirement : IAuthorizationRequirement
{
    public string Scope { get; }

    public HasScopeRequirement(string scope, string issuer)
    {
        Scope = scope ?? throw new ArgumentNullException(nameof(scope));
    }
}

public class HasScopeHandler : AuthorizationHandler<HasScopeRequirement>
{
    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, HasScopeRequirement requirement)
    {
        if (!context.User.HasClaim(c => c.Type == "scope"))
            return Task.CompletedTask;

        var scopes = context.User.FindFirst(c => c.Type == "scope").Value.Split(' ');

        if (scopes.Any(s => s == requirement.Scope))
            context.Succeed(requirement);

        return Task.CompletedTask;
    }
}
公共类要求:IAAuthorizationRequirement
{
公共字符串作用域{get;}
公共要求(字符串范围、字符串颁发者)
{
Scope=Scope??抛出新ArgumentNullException(nameof(Scope));
}
}
公共类HasScopeHandler:AuthorizationHandler
{
受保护的覆盖任务HandleRequirementAsync(授权HandlerContext上下文,HassCopeRequirementAsync)
{
if(!context.User.HasClaim(c=>c.Type==“scope”))
返回Task.CompletedTask;
var scopes=context.User.FindFirst(c=>c.Type==“scope”).Value.Split(“”);
if(scopes.Any(s=>s==requirement.Scope))
成功(要求);
返回Task.CompletedTask;
}
}
从这里,我们可以做到以下几点:

控制器

[ApiController]
[Authorize("Scope1")]
public class PingController : ControllerBase
{
    [HttpGet("ping")]
    public async Task<ActionResult> Ping()
    {
        return Ok("Ping pong");
    }
}
[ApiController]
[授权(“范围1”)]
公共类PingController:ControllerBase
{
[HttpGet(“ping”)]
公共异步任务Ping()
{
返回Ok(“乒乓球”);
}
}