IdentityServer4 -OpenID Connect 模式

1 什么是OIDC

OIDC 是對OpenID Connect 的檢查,OIDC=(Identity ,Autjentication)+OAuth 2.0.


OIDC

博文參考:https://www.cnblogs.com/linianhui/p/openid-connect-core.html

2 創(chuàng)建一個MVC 客戶端

授權(quán)服務(wù)端采用之前的服務(wù)端 (代碼已經(jīng)上傳,點擊下載) 服務(wù)中心項目為 IdentityServer.ServerMVC

2.1.1新建一個 ASP.NET Core 網(wǎng)站 IdentityServer.ClientMVC,選擇不要授權(quán)Authentication。

2.1.2修改Satrtup.cs文件中的 ConfigureServices方法:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

    services.AddAuthentication(options =>
        {
            options.DefaultScheme = "Cookies";
            options.DefaultChallengeScheme = "oidc";
        })
        .AddCookie("Cookies")
        .AddOpenIdConnect("oidc", options =>
        {
            options.SignInScheme = "Cookies";

            options.Authority = "http://localhost:5000";//授權(quán)服務(wù)中心
            options.RequireHttpsMetadata = false;

            options.ClientId = "mvc";//授權(quán)服務(wù)分配的ClientId
            options.SaveTokens = true;
        });
}
  • AddAuthentication 注入授權(quán)服務(wù)到容器中,用Cookie 作為驗證用戶的方法(通過Cookies設(shè)置于為DefaultScheme).
  • 設(shè)置DefaultChallengeScheme為oidc,因為在用戶登錄時,使用OpenID Connect 的防范。
  • 使用AddCookie添加為處理Cookie 的程序。
  • AddOpenIdConnect 用來配置OpenID Connect協(xié)議的處理程序。

同處修改 Configure方法,將授權(quán)加入中間件。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseAuthentication();

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

2.1.3 在需要認(rèn)證登錄的控制器或者Action加入Authorize標(biāo)簽。
在視圖中加入如下代碼,展示授權(quán)信息

<dl>
    @foreach (var claim in User.Claims)
    {
        <dt>@claim.Type</dt>
        <dd>@claim.Value</dd>
    }
</dl>

原文文檔:https://identityserver4.readthedocs.io/en/release/quickstarts/3_interactive_login.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容