一.概述

  在前二篇中讲到了客户端授权的二种方式: granttypes.clientcredentials凭据授权和granttypes.resourceownerpassword密码授权,都是oauth2.0协议。本篇使用openid connect添加用户认证,客户端授权是granttypes.implicit隐式流授权,是ocid协议。 本篇示例中只有二个项目:一个identityserver的mvc应用程序,一个客户端mvc应用程序(用户client端)。

    下面介绍身份认证交互流程:

      (1) 先启动identityserver程序http://localhost:5000

      (2) 启动客户端mvcclient程序http://localhost:5002

      (3) client用户访问http://localhost:5002/secure时,想获取个人信息和资料信息,如果用户没有进行身份认证,oidc会重定向

      (4) 重定向到identityserver服务端站点的登录页:http://localhost:5000/account/login?returnurl=xxx

  (5) 用户登录成功后。自动跳回到mvcclient客户端站点,访问地址http://localhost:5002/home/secure。获取了当前个人信息和资料信息

  上面的步骤了解到:client用户要访问个人信息时,必须先进行,交互式用户身份验证account/login,验证通过后,客户端浏览器会保存服务令牌在cookie中。 需要注意的是:在隐式授权中,令牌是通过浏览器传输,在mvcclient客户端程序中用httpclient获取cookie中的令牌来请求api,返回是http 401状态,这是因为该令牌是身份令牌还非访问令牌。

   从github中下载开源项目,可以快速入门启动openid connect协议的交互式用户身份验证支持。在实际项目中,也可以将示例中的控制器,视图,模型和css整合到自己项目的identityserver web应用程序中。

  

二. identityserver mvc应用程序

   因为是交互式用户身份验证,必须有ui界面,所以identityserver是一个mvc应用程序。下面是示例项目目录:

    account:客户端站点重定向到服务端站点,用于用户登录或注销。

    grants: 用于撤销客户端访问权限。

    consent :是用户登录成功后,跳转到授权许可的ui界面。用户可以决定是否要将他的身份信息发布到客户端应用程序。

    device :是设备流交互服务。

    diagnostics: 是诊断查看个人身份认证cookie信息。

 

  1.1 定义客户端

    在config.cs类中,定义客户端,将 openid connect隐式流添加到客户端。基于openid connect的客户端与oauth 2.0客户端非常相似。但由于oidc中的流程始终是交互式的,因此我们需要在配置中添加一些重定向url。

      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" }
                },
                // resource owner password grant client
                new client
                {
                    clientid = "ro.client",
                    allowedgranttypes = granttypes.resourceownerpassword,

                    clientsecrets =
                    {
                        new secret("secret".sha256())
                    },
                    allowedscopes = { "api1" }
                },
                // openid connect implicit flow client (mvc)
                new client
                {
                    clientid = "mvc",
                    clientname = "mvc client",
                    allowedgranttypes = granttypes.implicit,
                     
                    //oidc中的流程始终是交互式的
                    //登录后要重定向到哪里
                    redirecturis = { "http://localhost:5002/signin-oidc" },

                    // 注销后重定向到哪里
                    postlogoutredirecturis = { "http://localhost:5002/signout-callback-oidc" },

                    //与oauth 2.0类似,openid connect也使用范围概念,与oauth相比,oidc中的范围不代表api,而是代表用户id,名称或电子邮件地址等身份数据。
                    allowedscopes = new list<string>
                    {
                        //主题id,也是用户唯一id(最低要求)
                        identityserverconstants.standardscopes.openid,
                        //个人信息的claims,名称或电子邮件地址等身份数据
                        identityserverconstants.standardscopes.profile
                    }
                }
            };
        }

 

  1.2 定义oidc范围

        public static ienumerable<identityresource> getidentityresources()
        {
            return new list<identityresource>
            {
                new identityresources.openid(),
                new identityresources.profile(),
            };
        }

  

  1.3 在startup启动类中启动 identityserver服务

            var builder = services.addidentityserver()
                .addinmemoryidentityresources(config.getidentityresources())
                .addinmemoryapiresources(config.getapis())
                .addinmemoryclients(config.getclients())
                .addtestusers(config.getusers());    

 

二. mvcclient 客户端应用程序

  2.1 startup启动类

    添加对openid connect身份验证的支持,在启动时将以下代码添加到configureservices方法中:

       public void configureservices(iservicecollection services)
        {
            services.addmvc();
            //关闭了jwt声明类型映射
            jwtsecuritytokenhandler.defaultinboundclaimtypemap.clear();

            //添加authentication 到服务集合中
            services.addauthentication(options =>
                {
                    //使用cookie本地登录用户
                    options.defaultscheme = "cookies";
                    //用户登录时,使用openid连接协议。
                    options.defaultchallengescheme = "oidc";
                })
                //添加对cookie的处理支持
                .addcookie("cookies")
                //oidc处理程序
                .addopenidconnect("oidc", options =>
                {
                    //受信任的identityserver服务地址
                    options.authority = "http://localhost:5000";
                    options.requirehttpsmetadata = false;
                    //客户端标识
                    options.clientid = "mvc";
                    //将identityserver中的令牌持久化到cookie中(客户端浏览器中)
                    options.savetokens = true;
                    
                });
        }
        public void configure(iapplicationbuilder app, ihostingenvironment env)
        {
            if (env.isdevelopment())
            {
                app.usedeveloperexceptionpage();
            }
            else
            {
                app.useexceptionhandler("/home/error");
            }

            //每个请求都能执行身份验证服务
            app.useauthentication();

            app.usestaticfiles();
            app.usemvcwithdefaultroute();
        }    

  

  2.2 访问个人信息  

    由于使用的是openid connect,是基于浏览器的交互式身份认证。在action中添加一个[authorize],会触发身份验证握手。下面secure方法,显示当前用户的声明以及cookie属性。 握手时重定向到identityserver服务站点下进行登录。

      //身份验证握手,采用oidc,重定向到identityserver进行登录
        [authorize]
        public iactionresult secure()
        {
            viewdata["message"] = "secure page.";

            return view();
        }

    下面是secure视图:

@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>
    <!-- 显示身份认中的cookie -->
    @foreach (var prop in (await context.authenticateasync()).properties.items)
    {
        <dt>@prop.key</dt>
        <dd>@prop.value</dd>
    }
</dl>

   

  2.3 注销

    使用identityserver等身份验证服务,仅清除本地应用程序cookie是不够的(客户端浏览器)。此外,还需要向identityserver进行往返以清除中央单点登录会话。

    public iactionresult logout()
    {
        return signout("cookies", "oidc");
    }

    触发logout后,会清除本地cookie(客户端浏览器),然后重定向到identityserver。identityserver将清除其cookie(服务端浏览器),然后为用户提供返回mvc应用程序的链接。

 

 参考文献

  使用openid connect添加用户认证