mvc方案5、授权-接口-客户模式
new Client
{
// 客户端的唯一标识
ClientId="client",
// 客户端认证密码
ClientSecrets =
{
new Secret("ordersecret".Sha256())
},
// 指定授权模式,这里指定为客户端凭据模式
AllowedGrantTypes = GrantTypes.ClientCredentials,
// 指定客户端获取的Token能访问到的作用域
AllowedScopes={ "orderApi" }
},
接口端
// 注册认证相关组件和配置defaultScheme为Bearer
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
// 指定要接入的授权服务器地址
options.Authority = "http://localhost:6100";
// 在验证token时,不验证Audience
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = false
};
// 不适用Https
options.RequireHttpsMetadata = false;
});
services.AddAuthorization(options =>
{
options.AddPolicy("orderScope", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim("scope", "orderApi");
});
});
// 注册认证过滤器,在授权过滤器前面
app.UseAuthentication();
// 注册授权过滤器
app.UseAuthorization();
客户端
引入 oidc-client.js"
// 根据配置信息创建用户管理对象,后续直接使用
this.mgr = new Oidc.UserManager(this.config);
// 客户端配置
config: {
authority: "http://localhost:6100",
client_id: "JsClient",
redirect_uri: "http://localhost:8080/callback.html",
response_type: "id_token token",
scope: "openid profile orderApi", //客户端需要的权限范围
post_logout_redirect_uri: "http://localhost:8080/index.html",
},
mgr: null, //登录对象
results: "" //登录通过数据