asp.net core IdentityServer4 實現 implicit(隱式許可)實現第三方登錄


前言

OAuth 2.0默認四種授權模式(GrantType)

本章主要介紹簡化模式(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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM