使用IdentityServer4和Ocelot 自己core2.1时以前框架搭建并正常配置后出现了几个出现的问题,花了一些时间查到记录下
问题1.invalid_client
检查自己的ClientId和ClientSecret的是否正确
问题2. ocelot服务任何接口都返回404 NotFound
正确配置如下:主要原因:注意: ocelot16.x版本之后的配置节点写为Routes,而非ReRoutes
问题3:请求identityserver4出现异常invalid_scope
IdentityServer4 4.x版本跟老版本有些区别,可能你的学习资料还是老的
.AddInMemoryApiScopes(Config.ApiScopes) //这个方法是新加的
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetResource())
.AddInMemoryClients(Config.GetClients())
.AddInMemoryApiScopes(Config.ApiScopes) //这个方法是新加的
.AddTestUsers(Config.GetTestUsers());`
问题4:accesstoken正常返回就是请求api或者ocelot 返回:Unauthorized
感谢:.Net Core WebApi(三)--使用 IdentityServer4 4.1.1 踩坑记录 - C余L小R鱼 - 博客园
在identityserver4配置ApiResources时, 以前的版本可以直接一个new ApiResource("invoice", "Invoice API"),但是新版本需要以组合scope组合配置 如下图
//调取时 将多个scope的Name加参时 可能会很长很长 为了方便 我们将scope 进行分组 后期可直接调用**统称** 这里分成两组
public static readonly IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("invoice", "Invoice API")//设置每组scopes的统称
{
Scopes = { "invoice.read", "invoice.pay", "manage" }//设置每组scope具体名称
},
new ApiResource("customer", "Customer API")
{
Scopes = { "customer.read", "customer.contact", "manage" }
}
};
}