asp.net core IdentityServer4 實現 implicit(隱式許可)實現第三方登錄
- 2019 年 10 月 3 日
- 筆記
前言
OAuth 2.0默認四種授權模式(GrantType)
- 授權碼模式(authorization_code)
- 簡化模式(implicit)
- 密碼模式(resource owner password) credentials)
- 客戶端模式(client_credentials)
本章主要介紹簡化模式(implicit)
,不通過第三方應用程式的伺服器,直接在瀏覽器中向認證伺服器申請令牌,跳過了"授權碼"這個步驟,因此得名。所有步驟在瀏覽器中完成,令牌對訪問者是可見的,且客戶端不需要認證。
認證步驟
- 客戶端攜帶客戶端標識以及重定向URI到授權伺服器;
- 用戶確認是否要授權給客戶端;
- 授權伺服器得到許可後,跳轉到指定的重定向地址,並將令牌也包含在了裡面;
- 客戶端不攜帶上次獲取到的包含令牌的片段,去請求資源伺服器;
- 資源伺服器會向瀏覽器返回一個腳本;
- 瀏覽器會根據上一步返回的腳本,去提取在C步驟中獲取到的令牌;
- 瀏覽器將令牌推送給客戶端。
配置認證授權伺服器
Package
PM> Install-package IdentityServer4 -version 2.5.3
創建一個類Config(配置要保護的資源,和可以訪問的API的客戶端伺服器)
public class Config { /// <summary> /// 定義身份資源 /// </summary> /// <returns></returns> public static IEnumerable<IdentityResource> GetIdentityResources() { return new List<IdentityResource> { new IdentityResources.OpenId(), new IdentityResources.Profile(), new IdentityResources.Email() }; } /// <summary> /// 定義授權客戶端 /// </summary> /// <returns></returns> public static IEnumerable<Client> GetClients() { return new List<Client> { new Client{ ClientId="mvc", ClientName="MyClient", AllowedGrantTypes=GrantTypes.Implicit, RedirectUris = { "http://localhost:5003/signin-oidc" },//跳轉登錄到的客戶端的地址 PostLogoutRedirectUris = { "http://localhost:5003/signout-callback-oidc" },//跳轉登出到的客戶端的地址 AllowedScopes = new List<string> { IdentityServerConstants.StandardScopes.OpenId, IdentityServerConstants.StandardScopes.Profile, IdentityServerConstants.StandardScopes.Email }, RequireConsent=false } }; } }
配置Startup
再走到ConfigureServices方法注入IdentityServer4服務
services.AddIdentityServer() .AddDeveloperSigningCredential() .AddInMemoryIdentityResources(Config.GetIdentityResources()) .AddInMemoryClients(Config.GetClients()) .AddTestUsers(TestUsers.Users);
在Configure方法中添加IdentityServer4服務中間件
app.UseIdentityServer();
新建客戶端
配置Startup
再走到ConfigureServices方法注入IdentityServer4服務
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); services.AddAuthentication(options => { options.DefaultScheme = "Cookies"; options.DefaultChallengeScheme = "oidc"; }) .AddCookie("Cookies") .AddOpenIdConnect("oidc", options => { options.Authority = "http://localhost:5004"; options.RequireHttpsMetadata = false; options.ClientId = "mvc"; options.SaveTokens = true; options.GetClaimsFromUserInfoEndpoint = true; });
在Configure方法中添加認證服務中間件
app.UseAuthentication();
Run
添加第三方快捷登錄(github)
在授權伺服器ConfigureServices注入
直接貼程式碼吧
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddIdentityServer() .AddDeveloperSigningCredential() .AddInMemoryIdentityResources(Config.GetIdentityResources()) .AddInMemoryClients(Config.GetClients()) .AddTestUsers(TestUsers.Users); services.AddAuthentication().AddGitHub(options => { options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme; options.ClientId = "your client"; options.ClientSecret = "your Secret"; }); }
Run
登錄成功後可以獲取到聲明的ClaimsIdentity
頁面大家可以通過 https://github.com/IdentityServer/IdentityServer4.Templates進行下載
,或者通過命令dotnet new -i identityserver4.templates
進行下載
github 可以到
註冊完應用就會有應用編碼和密鑰了
概要
參考:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html
Demo:https://github.com/fhcodegit/IdentityServer4.Samples/tree/master/Quickstarts/ImplicitFlowAuthentication