asp.net core系列 56 IS4使用OpenID Connect添加用戶認證


一.概述

  在前二篇中講到了客戶端授權的二種方式: GrantTypes.ClientCredentials憑據授權和GrantTypes.ResourceOwnerPassword密碼授權,都是OAuth2.0協議。本篇使用OpenID Connect添加用戶認證,客戶端授權是GrantTypes.Implicit隱式流授權,是OCID協議。 本篇示例中只有二個項目:一個IdentityServer的mvc應用程序,一個客戶端mvc應用程序(用戶client端)。

    下面介紹身份認證交互流程:

      (1) 先啟動IdentityServer程序http://localhost:5000

      (2) 啟動客戶端MvcClient程序http://localhost:5002

      (3) Client用戶訪問http://localhost:5002/Secure時,想獲取個人信息和資料信息,如果用戶沒有進行身份認證,OIDC會重定向

      (4) 重定向到IdentityServer服務端站點的登錄頁:http://localhost:5000/Account/Login?ReturnUrl=xxx

  (5) 用戶登錄成功后。自動跳回到MvcClient客戶端站點,訪問地址http://localhost:5002/Home/Secure。獲取了當前個人信息和資料信息

  上面的步驟了解到:Client用戶要訪問個人信息時,必須先進行,交互式用戶身份驗證Account/Login,驗證通過后,客戶端瀏覽器會保存服務令牌在cookie中。 需要注意的是:在隱式授權中,令牌是通過瀏覽器傳輸,在MvcClient客戶端程序中用HttpClient獲取cookie中的令牌來請求api,返回是http 401狀態,這是因為該令牌是身份令牌還非訪問令牌。

   從Github中下載開源項目,可以快速入門啟動OpenID Connect協議的交互式用戶身份驗證支持。在實際項目中,也可以將示例中的控制器,視圖,模型和CSS整合到自己項目的IdentityServer Web應用程序中。

  

二. IdentityServer MVC應用程序

   因為是交互式用戶身份驗證,必須有UI界面,所以IdentityServer是一個MVC應用程序。下面是示例項目目錄:

    Account:客戶端站點重定向到服務端站點,用於用戶登錄或注銷。

    Grants: 用於撤銷客戶端訪問權限。

    Consent :是用戶登錄成功后,跳轉到授權許可的UI界面。用戶可以決定是否要將他的身份信息發布到客戶端應用程序。

    Device :是設備流交互服務。

    Diagnostics: 是診斷查看個人身份認證cookie信息。

 

  1.1 定義客戶端

    在config.cs類中,定義客戶端,將 OpenID Connect隱式流添加到客戶端。基於OpenID Connect的客戶端與OAuth 2.0客戶端非常相似。但由於OIDC中的流程始終是交互式的,因此我們需要在配置中添加一些重定向URL。

      public static IEnumerable<Client> GetClients()
        {
            return new List<Client>
            {
                new Client
                {
                    ClientId = "client",

                    // no interactive user, use the clientid/secret for authentication
                    AllowedGrantTypes = GrantTypes.ClientCredentials,

                    // secret for authentication
                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },

                    // scopes that client has access to
                    AllowedScopes = { "api1" }
                },
                // resource owner password grant client
                new Client
                {
                    ClientId = "ro.client",
                    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,

                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },
                    AllowedScopes = { "api1" }
                },
                // OpenID Connect implicit flow client (MVC)
                new Client
                {
                    ClientId = "mvc",
                    ClientName = "MVC Client",
                    AllowedGrantTypes = GrantTypes.Implicit,
                     
                    //OIDC中的流程始終是交互式的
                    //登錄后要重定向到哪里
                    RedirectUris = { "http://localhost:5002/signin-oidc" },

                    // 注銷后重定向到哪里
                    PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },

                    //與OAuth 2.0類似,OpenID Connect也使用范圍概念,與OAuth相比,OIDC中的范圍不代表API,而是代表用戶ID,名稱或電子郵件地址等身份數據
                    AllowedScopes = new List<string>
                    {
                        //主題id,也是用戶唯一ID(最低要求)
                        IdentityServerConstants.StandardScopes.OpenId,
                        //個人信息的claims,名稱或電子郵件地址等身份數據
                        IdentityServerConstants.StandardScopes.Profile
                    }
                }
            };
        }

 

  1.2 定義OIDC范圍

        public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            return new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
            };
        }

  

  1.3 在Startup啟動類中啟動 IdentityServer服務

            var builder = services.AddIdentityServer()
                .AddInMemoryIdentityResources(Config.GetIdentityResources())
                .AddInMemoryApiResources(Config.GetApis())
                .AddInMemoryClients(Config.GetClients())
                .AddTestUsers(Config.GetUsers());    

 

二. MvcClient 客戶端應用程序

  2.1 Startup啟動類

    添加對OpenID Connect身份驗證的支持,在啟動時將以下代碼添加到ConfigureServices方法中:

       public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            //關閉了JWT聲明類型映射
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

            //添加authentication 到服務集合中
            services.AddAuthentication(options =>
                {
                    //使用cookie本地登錄用戶
                    options.DefaultScheme = "Cookies";
                    //用戶登錄時,使用OpenID連接協議。
                    options.DefaultChallengeScheme = "oidc";
                })
                //添加對cookie的處理支持
                .AddCookie("Cookies")
                //oidc處理程序
                .AddOpenIdConnect("oidc", options =>
                {
                    //受信任的IdentityServer服務地址
                    options.Authority = "http://localhost:5000";
                    options.RequireHttpsMetadata = false;
                    //客戶端標識
                    options.ClientId = "mvc";
                    //將IdentityServer中的令牌持久化到cookie中(客戶端瀏覽器中)
                    options.SaveTokens = true;
                    
                });
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            //每個請求都能執行身份驗證服務
            app.UseAuthentication();

            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();
        }    

  

  2.2 訪問個人信息  

    由於使用的是OpenID Connect,是基於瀏覽器的交互式身份認證。在action中添加一個[Authorize],會觸發身份驗證握手。下面Secure方法,顯示當前用戶的聲明以及cookie屬性。 握手時重定向到IdentityServer服務站點下進行登錄。

      //身份驗證握手,采用oidc,重定向到IdentityServer進行登錄
        [Authorize]
        public IActionResult Secure()
        {
            ViewData["Message"] = "Secure page.";

            return View();
        }

    下面是Secure視圖:

@using Microsoft.AspNetCore.Authentication

<h2>Claims</h2>

<dl>
    <!-- 顯示用戶聲明-->
    @foreach (var claim in User.Claims)
    {
        <dt>@claim.Type</dt>
        <dd>@claim.Value</dd>
    }
</dl>

<h2>Properties</h2>

<dl>
    <!-- 顯示身份認中的cookie -->
    @foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items)
    {
        <dt>@prop.Key</dt>
        <dd>@prop.Value</dd>
    }
</dl>

   

  2.3 注銷

    使用IdentityServer等身份驗證服務,僅清除本地應用程序cookie是不夠的(客戶端瀏覽器)。此外,還需要向IdentityServer進行往返以清除中央單點登錄會話。

    public IActionResult Logout()
    {
        return SignOut("Cookies", "oidc");
    }

    觸發Logout后,會清除本地cookie(客戶端瀏覽器),然后重定向到IdentityServer。IdentityServer將清除其cookie(服務端瀏覽器),然后為用戶提供返回MVC應用程序的鏈接。

 

 參考文獻

  使用OpenID Connect添加用戶認證

 


免責聲明!

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



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