IdentityServer4入門一


這幾天學習IdentityServer4,感覺內容有點亂,也可能自己水平有限吧。但為了鞏固學習的內容,也打算自己理一下思路。

首先IdentityServer解決什么問題?

下圖是我們的一個程序的組織形式

 

 

詳情可以看看官網的描述:https://identityserver4.readthedocs.io/en/latest/intro/big_picture.html
我的理解是:IdentityServer就是解決多點登錄及API授權、WEB授權的問題

第一個例子

我們將重現官網上的第一個范例來學習相關概念,但與官網的不同,我打算一開始就將服務端從一個MVC網站開始。官網的第一個范例:https://identityserver4.readthedocs.io/en/latest/quickstarts/1_client_credentials.html

下面截圖和代碼來自VS.NET2019+asp.net core 3.0

新建服務端

新增asp.net core Web應用程序,項目名稱IdentityMvc。因為還要后面加上測試的客戶端,所以解決方案我使用了另外的一個名稱OpenIdConnect

 

 

 

 

 

 利用nuget添加(安裝)引用

IdentityServer4

 

 將端口修改一下,授權服務的端口我們使用44300。打開Properties\launchSettings.json文件

 

 

新增Config.cs文件

using IdentityServer4.Models;
using System.Collections.Generic;

namespace IdentityMvc
{
    public static class Config
    {
        public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            return new IdentityResource[]
            {
                new IdentityResources.OpenId()
            };
        }

        public static IEnumerable<ApiResource> GetApis()
        {
            return new List<ApiResource>
            {
                new ApiResource("api1", "My API")
            };
        }

        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" }
                }
            };
        }
    }
}

  

修改startup.cs文件

在ConfigureServices(IServiceCollection services)文件添加以下代碼

var builder = services.AddIdentityServer()
                .AddInMemoryIdentityResources(Config.GetIdentityResources())
                .AddInMemoryApiResources(Config.GetApiResources())
                .AddInMemoryClients(Config.GetClients());

  

在Configure(IApplicationBuilder app, IWebHostEnvironment env)方法,添加app.UseIdentityServer();

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        // The default HSTS value is 30 days. You may want to change thi
        app.UseHsts();
    }
    app.UseIdentityServer();//添加這一句

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    //...省略下方代碼

  

至此,保護API的服務端就做好了。我們可以點擊調試運行,IDE會打開IE並訪問home頁。home頁一般能正常打開,但如何測試授權服務是否正常呢,可以在地址欄添加.well-known/openid-configuration,應能看到類似的內容

 

  

 

上圖的地址的端口可能會有所不同。如果openid-configuration頁面看到是空白的話,估計我們少加入了app.UseIdentityServer()方法。

好了,授權服務端就這樣的了。接着就是需要一個API的服務程序,和一個調用API的客戶端。


免責聲明!

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



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