客户端应用程序如何使用从访问令牌中提取的范围和资源来限制对 API 的访问 - identityserver4
How client application uses the scopes and resources extracted from access token to restrict the access of API - identityserver4
我可以看到许多描述如何使用 identityserver4 的链接。
主机应用程序:
使用 [clientId, secret, APIScopes, APIResources, IdentityResources] 配置客户端
将客户端详细信息传递给 identityserver4
客户端应用程序:
将客户端 ID 传递给端点以获取访问令牌和刷新令牌,其中包含已定义客户端的范围和资源。使用该范围和资源,我们可以限制 API 的访问权限。
但我仍然想知道客户端应用程序将如何使用 API 范围来限制应用程序的访问是否有任何示例如何利用范围来限制应用程序访问?
以及在 identitserver4 中维护角色的方法
我找不到任何描述获取访问令牌后如何使用客户端部分的链接,请与我分享任何可以帮助我的参考资料?
在 API (AddJwtBearer) 中,您做两件事,身份验证和授权。
在授权阶段,您检查在访问令牌中找到的声明(范围是声明的一部分)。
可以使用基于角色或策略的方法进行授权检查。
示例策略可能如下所示:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthorization(options =>
{
options.AddPolicy("ViewReports", policy =>
policy.RequireAuthenticatedUser()
.RequireRole("Finance")
.RequireRole("Management")
);
});
然后你可以用这个属性装饰你的控制器:
[Authorize("ViewReports")]
public class SecretController : Controller
{
}
从消费者 (API) 的角度来看,范围与所有其他声明一样。他们没有区别对待。
使用identityserver4时,client需要配置ClientId和权限地址,本server需要配置clientid对应的allowed scope
services.AddAuthentication(config=>
{
config.DefaultScheme = "cookie";
config.DefaultChallengeScheme = "oidc";
})
.AddCookie("cookie")
.AddOpenIdConnect("oidc", config=>
{
config.Authority = "url";
config.ClientId = "client_id";
config.ClientSecret = "client_secret";
config.SaveTokens = true;
config.ResponseType = "code";
});
Identityserver会根据ClientId授权一些功能
new Client
{
ClientId="client_id",
ClientSecrets=
{
new Secret("client_secret".ToSha256())
},
AllowedGrantTypes=GrantTypes.Code,
RedirectUris={ "https://localhost:[port]/signin-oidc"},
AllowedScopes={ "apione", "apitwo",
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
//...
},
RequireConsent=false
}
在api一中,还需要配置听众。
services.AddAuthentication("jwtauth")
.AddJwtBearer("jwtauth",config=>
{
config.Authority = "identityserver url";
config.Audience = "apione";
config.RequireHttpsMetadata = false;
IdentityModelEventSource.ShowPII = true;
});
每个用户都有自己的角色,所以你可以在用户登录服务器后添加声明。此外,项目 api 可以按照 Tore Nestenius 的说法配置 AddAuthorization。
我可以看到许多描述如何使用 identityserver4 的链接。
主机应用程序: 使用 [clientId, secret, APIScopes, APIResources, IdentityResources] 配置客户端 将客户端详细信息传递给 identityserver4
客户端应用程序: 将客户端 ID 传递给端点以获取访问令牌和刷新令牌,其中包含已定义客户端的范围和资源。使用该范围和资源,我们可以限制 API 的访问权限。
但我仍然想知道客户端应用程序将如何使用 API 范围来限制应用程序的访问是否有任何示例如何利用范围来限制应用程序访问?
以及在 identitserver4 中维护角色的方法
我找不到任何描述获取访问令牌后如何使用客户端部分的链接,请与我分享任何可以帮助我的参考资料?
在 API (AddJwtBearer) 中,您做两件事,身份验证和授权。
在授权阶段,您检查在访问令牌中找到的声明(范围是声明的一部分)。
可以使用基于角色或策略的方法进行授权检查。
示例策略可能如下所示:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthorization(options =>
{
options.AddPolicy("ViewReports", policy =>
policy.RequireAuthenticatedUser()
.RequireRole("Finance")
.RequireRole("Management")
);
});
然后你可以用这个属性装饰你的控制器:
[Authorize("ViewReports")]
public class SecretController : Controller
{
}
从消费者 (API) 的角度来看,范围与所有其他声明一样。他们没有区别对待。
使用identityserver4时,client需要配置ClientId和权限地址,本server需要配置clientid对应的allowed scope
services.AddAuthentication(config=>
{
config.DefaultScheme = "cookie";
config.DefaultChallengeScheme = "oidc";
})
.AddCookie("cookie")
.AddOpenIdConnect("oidc", config=>
{
config.Authority = "url";
config.ClientId = "client_id";
config.ClientSecret = "client_secret";
config.SaveTokens = true;
config.ResponseType = "code";
});
Identityserver会根据ClientId授权一些功能
new Client
{
ClientId="client_id",
ClientSecrets=
{
new Secret("client_secret".ToSha256())
},
AllowedGrantTypes=GrantTypes.Code,
RedirectUris={ "https://localhost:[port]/signin-oidc"},
AllowedScopes={ "apione", "apitwo",
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
//...
},
RequireConsent=false
}
在api一中,还需要配置听众。
services.AddAuthentication("jwtauth")
.AddJwtBearer("jwtauth",config=>
{
config.Authority = "identityserver url";
config.Audience = "apione";
config.RequireHttpsMetadata = false;
IdentityModelEventSource.ShowPII = true;
});
每个用户都有自己的角色,所以你可以在用户登录服务器后添加声明。此外,项目 api 可以按照 Tore Nestenius 的说法配置 AddAuthorization。