网上关于Identity Server4的资料有挺多的,以前是一直看杨旭老师的,最近项目中有使用到,在使用.NET Core3.1的时候有一些不一样。因此在此记录一下。html
预备知识: http://www.javashuo.com/article/p-tepyssjv-q.htmlgit
本文内容参考程序员
杨旭老师以前博客:http://www.javashuo.com/article/p-csqeiuoa-de.htmlgithub
如杨旭老师所说,官方文档真的很详细,有时间建议你们看下官方文档。api
创建ASP.Net Core项目使用空模板。服务器
项目创建以后,运行方式改成使用控制台运行而不是IIS Express,以便查看各类debug信息。app
这个已成为习惯,也是学习杨老师的,确实比较方便,固然若是不喜欢能够不设置,只须要端口号配置的时候对应好就能够的。asp.net
修改后文件代码为:async
{ "profiles": { "IdentityServer4.AuthServer": { "commandName": "Project", "launchBrowser": true, "applicationUrl": "http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
端口号为5000
,此时运行程序,会显示出Hello World!
,默认的,没有修改。
点击安装就好啦。
API的配置和以前有所不一样,以前是
ApiResources
,如今分为ApiResources
和ApiScopes
,后续会说到。
using IdentityServer4.Models; using IdentityServer4.Test; using System.Collections.Generic; namespace IdentityServer4.AuthServer.Configuration { public class InMemoryConfiguration { /// <summary> /// Api Scopes /// </summary> /// <returns></returns> public static IEnumerable<ApiScope> ApiScopes() { return new List<ApiScope> { new ApiScope("scope1","scope1") }; } /// <summary> /// ApiResources /// </summary> /// <returns></returns> public static IEnumerable<ApiResource> ApiResources() { return new[] { new ApiResource { Name = "api1", DisplayName = "My Api1", Scopes = { "scope1" } } }; } /// <summary> /// Clients /// </summary> /// <returns></returns> public static IEnumerable<Client> Clients() { return new[] { new Client { ClientId = "client", AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials, ClientSecrets = { new Secret("secret".Sha256()) }, AllowedScopes = { "scope1" } } }; } /// <summary> /// Users /// </summary> /// <returns></returns> public static IEnumerable<TestUser> Users() { return new[] { new TestUser { SubjectId = "1", Username = "mail@qq.com", Password = "password" } }; } } }
ApiScopes
: 这个应该怎么翻译我也不清楚,API范围?若是没理解错的话,就是给以前的ApiResources
进行了一个分组。受权的时候会验证Scope
。
ApiResources
:好比官网的第一个demo,可能会有疑问,你怎么知道我是api1呢?其实,就没有验证,只要有受权码就能够访问的。若是说,我只要api1的话,那就用到ApiResources
了,生产环境中,也必然是须要用到的。
修改Startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddIdentityServer() .AddDeveloperSigningCredential() .AddTestUsers(InMemoryConfiguration.Users().ToList()) .AddInMemoryClients(InMemoryConfiguration.Clients()) .AddInMemoryApiScopes(InMemoryConfiguration.ApiScopes()) .AddInMemoryApiResources(InMemoryConfiguration.ApiResources()); }
固然,也须要app.UseIdentityServer();
首次启动时,Identity Server4将建立一个开发人员签名密钥,该文件名为
tempkey.rsa
。没必要将该文件签入源代码管理中,若是不存在该文件将被从新建立。也就是AddDeveloperSigningCredential()
。 这个方法只适合用于Identity Server4在单个机器运行, 若是是生产环境你得使用AddSigningCredential()
这个方法.
运行一下,发现并无什么改变,不过打开:http://localhost:5000/.well-known/openid-configuration,则应该看到所谓的发现文档。发现文档是身份服务器中的标准端点。客户端和API将使用发现文档来下载必要的配置数据。
打开Postman,按照配置的输入而后试一下
获取到Token,控制台输出以下:
这里是有用户的信息的,可是咱们能够把用户信息去掉,而后GrantType
改成client_credentials
,咱们设置的是 ResourceOwnerPasswordAndClientCredentials
这个GrantType
,因此使用用户名密码以及使用ClientCredentials
均可以。
不过此时控制台会有区别,没有用户信息了。
Identity Server 4 提供了一套QuickStart UI
https://github.com/IdentityServer/IdentityServer4.Quickstart.UI
此存储库包含UI所需的控制器,模型,视图和CSS文件。只需下载/克隆并将其复制到Web项目中便可。
打开项目根目录,运行Powershell,而后输入命令:
iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/IdentityServer/IdentityServer4.Quickstart.UI/main/getmain.ps1'))
不过可能你会遇到我前三次那种错误,嗯,访问不了,那就全局或者先下载下来人工粘贴过去吧~
好了之后咱们的项目是酱紫的:
因为有wwwroot下不少静态文件, 因此asp.net core 须要启用服务静态文件的功能: 修改Startup的Configure方法
先看修改前的样子吧
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseIdentityServer(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); }); }); }
修改后
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseIdentityServer(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}" ); }); }
是否是抛异常了?
由于咱们如今有UI了,因此不要忘记在ConfigureServices
里面注册MVC。
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddIdentityServer() .AddDeveloperSigningCredential() .AddTestUsers(InMemoryConfiguration.Users().ToList()) .AddInMemoryClients(InMemoryConfiguration.Clients()) .AddInMemoryApiScopes(InMemoryConfiguration.ApiScopes()) .AddInMemoryApiResources(InMemoryConfiguration.ApiResources()); }
而后运行一下试试:
登陆一下~
好了,如今咱们已经能够登陆成功了。
登陆界面能够自定义的~,OK,今天就到这里
接下来会说一下
推广下本身的公众号一个逗逼的程序员
,主要记录本身工做中解决问题的思路分享及学习过程当中的笔记。绝对不会程序员贩卖程序员的焦虑来割韭菜
。