asp.net core系列 56 IS4使用OpenID Connect添加用户认证

一.概述

  在前二篇中讲到了客户端授权的二种方式: 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添加用户认证

 

posted on 2019-04-17 14:53  花阴偷移  阅读(1229)  评论(0编辑  收藏  举报

导航