IdentityServer4入門一

這幾天學習IdentityServer4,感受內容有點亂,也可能本身水平有限吧。但爲了鞏固學習的內容,也打算本身理一下思路。html

首先IdentityServer解決什麼問題?json

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

 

 

詳情能夠看看官網的描述:https://identityserver4.readthedocs.io/en/latest/intro/big_picture.html
個人理解是:IdentityServer就是解決多點登陸及API受權、WEB受權的問題app

第一個例子asp.net

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

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

新建服務端測試

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

 

 

 

 

 

 利用nuget添加(安裝)引用ui

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的客戶端。

相關文章
相關標籤/搜索