溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

IdentityServer4如何使用OpenID Connect添加用戶身份驗(yàn)證

發(fā)布時(shí)間:2021-11-10 17:25:30 來(lái)源:億速云 閱讀:279 作者:柒染 欄目:大數(shù)據(jù)

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

使用IdentityServer4 實(shí)現(xiàn)OpenID Connect服務(wù)端,添加用戶身份驗(yàn)證。客戶端調(diào)用,實(shí)現(xiàn)授權(quán)。

IdentityServer4 目前已更新至1.0 版

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

下面正式開(kāi)始。

新建IdentityServer4服務(wù)端

服務(wù)端也就是提供服務(wù),如QQ Weibo等。

新建一個(gè)ASP.NET Core Web Application 項(xiàng)目IdentityServer4OpenID,選擇模板Web 應(yīng)用程序 不進(jìn)行身份驗(yàn)證。

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

添加IdentityServer4 引用:

Install-Package IdentityServer4

然后添加配置類Config.cs:

public class Config

    {

        //定義系統(tǒng)中的資源

        public static IEnumerable<IdentityResource> GetIdentityResources()

        {

            return new List<IdentityResource>

            {

                new IdentityResources.OpenId(),

                new IdentityResources.Profile(),

            };

        }

        public static IEnumerable<Client> GetClients()

        {

            // 客戶端憑據(jù)

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

                    //運(yùn)行訪問(wèn)的資源

                    AllowedScopes =

                    {

                        IdentityServerConstants.StandardScopes.OpenId,

                        IdentityServerConstants.StandardScopes.Profile

                    }

                }

            };

        }

        //測(cè)試用戶

        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測(cè)試數(shù)據(jù)類添加數(shù)據(jù),直接存在內(nèi)存中。IdentityServer4 是支持持久化。

然后打開(kāi)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部分也可以自己編寫,也就是登錄 注銷 允許和錯(cuò)誤。

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

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

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

在項(xiàng)目目錄下打開(kāi)命令提示符,輸入以上命令。

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

新建MVC客戶端

接著新建一個(gè)MVC客戶端,可以理解為你自己的應(yīng)用,需要使用第三方提供的服務(wù)。

新建一個(gè)ASP.NET Core Web Application 項(xiàng)目MvcClient,選擇模板Web 應(yīng)用程序 不進(jìn)行身份驗(yàn)證。

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

然后添加引用:

Install-Package Microsoft.AspNetCore.Authentication.Cookies

Install-Package Microsoft.AspNetCore.Authentication.OpenIdConnect

本文最終所引用的為1.1 。

接著打開(kāi)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)建的,如沒(méi)有可以自行創(chuàng)建。

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

<dl>

    @foreach (var claim in User.Claims)

    {

        <dt>@claim.Type</dt>

        <dd>@claim.Value</dd>

    }

</dl>

運(yùn)行

首先運(yùn)行服務(wù)端,定位到項(xiàng)目目錄下dotnet run,運(yùn)行起服務(wù)端以后,訪問(wèn)http://localhost:5000 ,確認(rèn)是否正常訪問(wèn)。

能正常訪問(wèn)接著運(yùn)行客戶端,同樣是dotnet run ,然后訪問(wèn)http://localhost:5002,會(huì)默認(rèn)跳轉(zhuǎn)至http://localhost:5000 ,這樣也就對(duì)了。

最終效果如下:

IdentityServer4如何使用OpenID Connect添加用戶身份驗(yàn)證

這里UI部分就是官方UI,我們也可以自行設(shè)計(jì)應(yīng)用到自己的系統(tǒng)中。登錄的用戶是配置的測(cè)試用戶,授權(quán)以后可以看到配置的Claims。

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI