IdentityServer4如何使用OpenIDConnect添加用戶身份驗證

IdentityServer4如何使用OpenID Connect添加用戶身份驗證,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

站在用戶的角度思考問題,與客戶深入溝通,找到長豐網站設計與長豐網站推廣的解決方案,憑借多年的經驗,讓設計與互聯網技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都做網站、網站建設、企業(yè)官網、英文網站、手機端網站、網站推廣、域名與空間、虛擬空間、企業(yè)郵箱。業(yè)務覆蓋長豐地區(qū)。

使用IdentityServer4 實現OpenID Connect服務端,添加用戶身份驗證??蛻舳苏{用,實現授權。

IdentityServer4 目前已更新至1.0 版

本文環(huán)境:IdentityServer4 1.0  .NET Core 1.0.1

下面正式開始。

新建IdentityServer4服務端

服務端也就是提供服務,如QQ Weibo等。

新建一個ASP.NET Core Web Application 項目IdentityServer4OpenID,選擇模板Web 應用程序 不進行身份驗證。

刪除模板創(chuàng)建的Controllers 文件以及Views 文件夾。

添加IdentityServer4 引用:

Install-Package IdentityServer4

然后添加配置類Config.cs:

public class Config

    {

        //定義系統中的資源

        public static IEnumerable<IdentityResource> GetIdentityResources()

        {

            return new List<IdentityResource>

            {

                new IdentityResources.OpenId(),

                new IdentityResources.Profile(),

            };

        }

        public static IEnumerable<Client> GetClients()

        {

            // 客戶端憑據

            return new List<Client>

            {

                // OpenID Connect implicit 客戶端 (MVC)

                new Client

                {

                    ClientId = "mvc",

                    ClientName = "MVC Client",

                    AllowedGrantTypes = GrantTypes.Implicit,

                    RedirectUris = { "http://localhost:5002/signin-oidc" },

                    PostLogoutRedirectUris = { "http://localhost:5002" },

                    //運行訪問的資源

                    AllowedScopes =

                    {

                        IdentityServerConstants.StandardScopes.OpenId,

                        IdentityServerConstants.StandardScopes.Profile

                    }

                }

            };

        }

        //測試用戶

        public static List<TestUser> GetUsers()

        {

            return new List<TestUser>

            {

                new TestUser

                {

                    SubjectId = "1",

                    Username = "admin",

                    Password = "123456",

                    Claims = new List<Claim>

                    {

                        new Claim("name", "admin"),

                        new Claim("website", "https://www.cnblogs.com/linezero")

                    }

                },

                new TestUser

                {

                    SubjectId = "2",

                    Username = "linezero",

                    Password = "123456",

                    Claims = new List<Claim>

                    {

                        new Claim("name", "linezero"),

                        new Claim("website", "https://github.com/linezero")

                    }

                }

            };

        }

    }

以上使用IdentityServer4測試數據類添加數據,直接存在內存中。IdentityServer4 是支持持久化。

然后打開Startup.cs 加入如下:

public void ConfigureServices(IServiceCollection services)

        {

            // Add framework services.

            services.AddMvc();

            services.AddIdentityServer()

                .AddTemporarySigningCredential()

                .AddInMemoryIdentityResources(Config.GetIdentityResources())

                .AddInMemoryClients(Config.GetClients())

                .AddTestUsers(Config.GetUsers());

        }

       public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

        {

            ...

            app.UseIdentityServer();

            ...

接著安裝UI,UI部分也可以自己編寫,也就是登錄 注銷 允許和錯誤。

可以到 https://github.com/IdentityServer/IdentityServer4.Quickstart.UI/tree/release 下載,然后解壓到項目目錄下。

也可以使用命令提示符快速安裝:

powershell iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/IdentityServer/IdentityServer4.Quickstart.UI/release/get.ps1'))

在項目目錄下打開命令提示符,輸入以上命令。

更多信息,可以查看官方readme:https://github.com/IdentityServer/IdentityServer4.Quickstart.UI/blob/release/README.md

新建MVC客戶端

接著新建一個MVC客戶端,可以理解為你自己的應用,需要使用第三方提供的服務。

新建一個ASP.NET Core Web Application 項目MvcClient,選擇模板Web 應用程序 不進行身份驗證。

配置Url 綁定5002端口 UseUrls("http://localhost:5002")

然后添加引用:

Install-Package Microsoft.AspNetCore.Authentication.Cookies

Install-Package Microsoft.AspNetCore.Authentication.OpenIdConnect

本文最終所引用的為1.1 。

接著打開Startup類,在Configure方法中添加如下代碼:

app.UseCookieAuthentication(new CookieAuthenticationOptions

            {

                AuthenticationScheme = "Cookies"

            });

            app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions

            {

                AuthenticationScheme = "oidc",

                SignInScheme = "Cookies",

                Authority = "http://localhost:5000",

                RequireHttpsMetadata = false,

                ClientId = "mvc",

                SaveTokens = true

            });

然后在HomeController 加上[Authorize] 特性,HomeController是VS2015 模板創(chuàng)建的,如沒有可以自行創(chuàng)建。

然后更改Home文件夾下的Index視圖如下:

<dl>

    @foreach (var claim in User.Claims)

    {

        <dt>@claim.Type</dt>

        <dd>@claim.Value</dd>

    }

</dl>

運行

首先運行服務端,定位到項目目錄下dotnet run,運行起服務端以后,訪問http://localhost:5000 ,確認是否正常訪問。

能正常訪問接著運行客戶端,同樣是dotnet run ,然后訪問http://localhost:5002,會默認跳轉至http://localhost:5000 ,這樣也就對了。

最終效果如下:

IdentityServer4如何使用OpenID Connect添加用戶身份驗證

這里UI部分就是官方UI,我們也可以自行設計應用到自己的系統中。登錄的用戶是配置的測試用戶,授權以后可以看到配置的Claims。

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯行業(yè)資訊頻道,感謝您對創(chuàng)新互聯的支持。

網頁名稱:IdentityServer4如何使用OpenIDConnect添加用戶身份驗證
標題URL:http://muchs.cn/article0/gphjio.html

成都網站建設公司_創(chuàng)新互聯,為您提供網站策劃品牌網站制作、定制開發(fā)搜索引擎優(yōu)化、網頁設計公司虛擬主機

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

成都定制網站建設