使用IdentityServer4 实现OpenID Connect服务端,添加用户身份验证。客户端调用,实现受权。html
IdentityServer4 目前已更新至1.0 版,在以前的文章中有所介绍。IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习保护API 。git
本文环境:IdentityServer4 1.0 .NET Core 1.0.1github
下面正式开始。web
新建IdentityServer4服务端
服务端也就是提供服务,如QQ Weibo等。shell
新建一个ASP.NET Core Web Application 项目IdentityServer4OpenID,选择模板Web 应用程序 不进行身份验证。mvc
删除模板建立的Controllers 文件以及Views 文件夹。app
添加IdentityServer4 引用:框架
Install-Package IdentityServer4ide
而后添加配置类Config.cs:post
public class Config { //定义系统中的资源 public static IEnumerable<IdentityResource> GetIdentityResources() { return new List<IdentityResource> { new IdentityResources.OpenId(), new IdentityResources.Profile(), }; } public static IEnumerable<Client> GetClients() { // 客户端凭据 return new List<Client> { // OpenID Connect implicit 客户端 (MVC) new Client { ClientId = "mvc", ClientName = "MVC Client", AllowedGrantTypes = GrantTypes.Implicit, RedirectUris = { "http://localhost:5002/signin-oidc" }, PostLogoutRedirectUris = { "http://localhost:5002" }, //运行访问的资源 AllowedScopes = { IdentityServerConstants.StandardScopes.OpenId, IdentityServerConstants.StandardScopes.Profile } } }; } //测试用户 public static List<TestUser> GetUsers() { return new List<TestUser> { new TestUser { SubjectId = "1", Username = "admin", Password = "123456", Claims = new List<Claim> { new Claim("name", "admin"), new Claim("website", "https://www.cnblogs.com/linezero") } }, new TestUser { SubjectId = "2", Username = "linezero", Password = "123456", Claims = new List<Claim> { new Claim("name", "linezero"), new Claim("website", "https://github.com/linezero") } } }; } }
以上使用IdentityServer4测试数据类添加数据,直接存在内存中。IdentityServer4 是支持持久化。
而后打开Startup.cs 加入以下:
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); services.AddIdentityServer() .AddTemporarySigningCredential() .AddInMemoryIdentityResources(Config.GetIdentityResources()) .AddInMemoryClients(Config.GetClients()) .AddTestUsers(Config.GetUsers()); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... app.UseIdentityServer(); ...
接着安装UI,UI部分也能够本身编写,也就是登陆 注销 容许和错误。
能够到 https://github.com/IdentityServer/IdentityServer4.Quickstart.UI/tree/release 下载,而后解压到项目目录下。
也能够使用命令提示符快速安装:
powershell iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/IdentityServer/IdentityServer4.Quickstart.UI/release/get.ps1'))
在项目目录下打开命令提示符,输入以上命令。
更多信息,能够查看官方readme:https://github.com/IdentityServer/IdentityServer4.Quickstart.UI/blob/release/README.md
新建MVC客户端
接着新建一个MVC客户端,能够理解为你本身的应用,须要使用第三方提供的服务。
新建一个ASP.NET Core Web Application 项目MvcClient,选择模板Web 应用程序 不进行身份验证。
配置Url 绑定5002端口 UseUrls("http://localhost:5002")
而后添加引用:
Install-Package Microsoft.AspNetCore.Authentication.Cookies
Install-Package Microsoft.AspNetCore.Authentication.OpenIdConnect
本文最终所引用的为1.1 。
接着打开Startup类,在Configure方法中添加以下代码:
app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = "Cookies" }); app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions { AuthenticationScheme = "oidc", SignInScheme = "Cookies", Authority = "http://localhost:5000", RequireHttpsMetadata = false, ClientId = "mvc", SaveTokens = true });
而后在HomeController 加上[Authorize] 特性,HomeController是VS2015 模板建立的,如没有能够自行建立。
而后更改Home文件夹下的Index视图以下:
<dl> @foreach (var claim in User.Claims) { <dt>@claim.Type</dt> <dd>@claim.Value</dd> } </dl>
运行
首先运行服务端,定位到项目目录下dotnet run,运行起服务端之后,访问http://localhost:5000 ,确认是否正常访问。
能正常访问接着运行客户端,一样是dotnet run ,而后访问http://localhost:5002,会默认跳转至http://localhost:5000 ,这样也就对了。
最终效果以下:
这里UI部分就是官方UI,咱们也能够自行设计应用到本身的系统中。登陆的用户是配置的测试用户,受权之后能够看到配置的Claims。
本文所采用的 Grant 为 Implicit,更为详细的OAuth 2.0 https://tools.ietf.org/html/rfc6749 。
示例GitHub:https://github.com/linezero/Blog/tree/master/IdentityServer4OpenID
参考官方文档:https://identityserver4.readthedocs.io/en/release/quickstarts/3_interactive_login.html
转自:https://www.cnblogs.com/linezero/p/identityserver4openidconnect.html