IdentityServer4入門四:應用Implicit模式保護網站(上)

咱們先新增一個網站,名爲「ClientMvc",也是asp.net core Web應用程序(模型視圖控制器)html

使用nuget安裝如下引用數據庫

Microsoft.AspNetCore.Authentication.Cookiesjson

Microsoft.AspNetCore.Authentication.OpenIdConnectapi

打開Properties\launchSettings.json,修改端口爲44302服務器

 

 

 

咱們修改該網站的Home頁,打開View/Home/Index.cshtml,使用如下內容替換mvc

@using Microsoft.AspNetCore.Authentication

<h2>Claims</h2>

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

<h2>Properties</h2>

<dl>
    @foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items)
    {
        <dt>@prop.Key</dt>
        <dd>@prop.Value</dd>
    }
</dl>

  修改控制器,加上Authorize屬性app

 

 

 一樣須要調整startup.cs的兩個方法asp.net

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

    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

    IdentityModelEventSource.ShowPII = true;

    services.AddAuthentication(options =>
        {
            options.DefaultScheme = "Cookies";
            options.DefaultChallengeScheme = "oidc";
        })
        .AddCookie("Cookies")
        .AddOpenIdConnect("oidc", options =>
        {
            options.SignInScheme = "Cookies";
            options.Authority = "https://localhost:44300";
            options.RequireHttpsMetadata = true;
            options.ClientId = "mvc";
            options.SaveTokens = true;
        });
}

  Configure方法,增長app.UseAuthentication();網站

MVC的網站調整好了,如今若是運行該網站的話,會提示錯誤ui

 

 

 好了,如今須要去爲咱們的認證服務器加上Implicit模式的支持

在Config.cs上需修改兩處

1.加上相應的Client。

2.添加IdentityResource

如下是整個文件代碼

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

namespace IdentityMvc
{
    public static class Config
    {
        public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            return new IdentityResource[]
            {
                new IdentityResources.OpenId(),
                //Implicit need it.
                new IdentityResources.Profile()
            };
        }

        public static IEnumerable<ApiResource> GetApiResources()
        {
            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" }
                },
                new Client
                {
                    ClientId = "mvc",
                    ClientName = "MVC Client",                    
                    AllowedGrantTypes = GrantTypes.Implicit,
                    RequireConsent=false,//不須要顯示否贊成受權 頁面 這裏就設置爲false                    
                    RedirectUris = { "https://localhost:44302/signin-oidc" },//登陸成功後返回的客戶端地址
                    PostLogoutRedirectUris = { "https://localhost:44302/signout-callback-oidc" },//註銷登陸後返回的客戶端地址
                    AllowedScopes =
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        IdentityServerConstants.StandardScopes.Email,

                        "api1", "api2.read_only"
                    },
                }
            };
        }
    }
}

  

修改項目爲多啓動項目

鼠標右鍵點擊」解決方案」,選擇屬性

 

 按上圖啓動後,你會發現IE打開兩個page,且都訪問了44300端口

 

至此,44302的首頁處於認證保護之下了。下一步就是回到44300去實現Account控制器的Login方法,完成整個認證過程。由於要讀取數據庫,內容比較多,另起一篇來講明過程。

相關文章
相關標籤/搜索