七、OIDC
在 OAuth 中,这些授权被称为scope。
OpenID-Connect也有自己特殊的scope--openid ,
它必须在第一次请求“身份鉴别服务器”(Identity Provider,简称IDP)时发送过去。
一、项目结构搭建大体如下:
1、项目搭建
在 IdentityS4 中定义了 Config 文件,用于Id4的配置,主要功能是为了认证模型,其中还设置了Client请求文中的 ClientId 这些参数等等。
那么 IdentItyAPI 就是我们的项目服务,其中通过 Authorize 标记的都是具有安全保护的API控制器,那么我们就需要去获取我们的IdentityS4 中的验证,这样我们才可以访问,那么我们就用了.NET Core控制台程序去模拟了这个过程,其中涉及了 HttpClient 相关知识。那么最后返回了我们的相关 token ,这样,我们可以根据 token 去获取我们想要的API服务
2、定义认证服务中心
1、设置默认端口
2、新建一个配置文件类:Config.cs 代码如下:
using IdentityServer4; using IdentityServer4.Models; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace IdentityS4 { public class Config { // scopes define the resources in your system public static IEnumerable<IdentityResource> GetIdentityResources() { return new List<IdentityResource> { new IdentityResources.OpenId(), new IdentityResources.Profile(), }; } // clients want to access resources (aka scopes) public static IEnumerable<Client> GetClients() { return new List<Client> { // OpenID Connect隐式流客户端(MVC) new Client { ClientId = "mvc", ClientName = "MVC Client", AllowedGrantTypes = GrantTypes.Implicit,//隐式方式 RequireConsent=false,//如果不需要显示否同意授权 页面 这里就设置为false RedirectUris = { "http://localhost:5002/signin-oidc" },//登录成功后返回的客户端地址 PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },//注销登录后返回的客户端地址 AllowedScopes =//下面这两个必须要加吧 不太明白啥意思 { IdentityServerConstants.StandardScopes.OpenId, IdentityServerConstants.StandardScopes.Profile } } }; } } }
3、在Startup.cs的ConfigureServices方法中注入Ids4服务,如下面红色部分代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddIdentityServer()//Ids4服务
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryClients(Config.GetClients());//把配置文件的Client配置资源放到内存
}
在Startup.cs的Configure方法中添加ids4服务中间件(注意要放在UseMvc之前就可以):
app.UseIdentityServer();
报错
解决方案
加上这个
http://localhost:5000/.well-known/openid-configuration
就能正常访问
二、关于页面跳转的默认是ids Account/Login ,在服务端更改为