栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

IdentityServer资源所有者密码凭证(密码模式)

IdentityServer资源所有者密码凭证(密码模式)

参考链接:创建使用[ResourceOwnerPassword-资源所有者密码凭证]授权模式的客户端
基本步骤:

  1. identityServer添加新客户端client(由于是内存模式,需要提前约束)
  2. 增加用户user及用户信息
  3. .AddTestUsers(IdpConfig.Users);(使得identityServer支持验证用户信息)
  4. 用户信息的资源配置(确定哪些用户能够获取哪些资源)
  5. startUp中添加.AddInMemoryApiResources(IdpConfig.GetApis())(把用户信息的集合配置到startUp中)
  6. 增加客户端配置(新建客户端项目)
  7. 密码模式获取Token
  8. 获取用户信息
  9. 准备Api资源
  10. 开始测试
补充说明(一)Config.cs代码:
using IdentityModel;
using IdentityServer4.Models;
using IdentityServer4.Test;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace WFinancial
{
    public class Config
    {
    }
}
public class IdpConfig
{
    /// 
    /// 用户认证信息
    /// 
    /// 
    public static IEnumerable GetApiResources()
    {
        return new List
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                new IdentityResources.Address(),
                new IdentityResources.Email(),
                new IdentityResources.Phone()
            };
    }


    /// 
    /// 用户集合
    /// 
    public static List Users =>
        new List
        {
                new TestUser{SubjectId = "818727", Username = "alice", Password = "alice",
                    Claims =
                    {
                        new Claim(JwtClaimTypes.Name, "Alice Smith"),
                        new Claim(JwtClaimTypes.GivenName, "Alice"),
                        new Claim(JwtClaimTypes.FamilyName, "Smith"),
                        new Claim(JwtClaimTypes.Email, "AliceSmith@email.com"),
                        new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
                        new Claim(JwtClaimTypes.WebSite, "http://alice.com"),
                        new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServer4.IdentityServerConstants.ClaimValueTypes.Json)
                    }
                },
                new TestUser{SubjectId = "88421113", Username = "bob", Password = "bob",
                    Claims =
                    {
                        new Claim(JwtClaimTypes.Name, "Bob Smith"),
                        new Claim(JwtClaimTypes.GivenName, "Bob"),
                        new Claim(JwtClaimTypes.FamilyName, "Smith"),
                        new Claim(JwtClaimTypes.Email, "BobSmith@email.com"),
                        new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
                        new Claim(JwtClaimTypes.WebSite, "http://bob.com"),
                        new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServer4.IdentityServerConstants.ClaimValueTypes.Json),
                        new Claim("location", "somewhere")
                    }
                }
        };



    /// 
    /// API 资源
    /// 
    /// 
    public static IEnumerable GetApis()
    {
        return new List
            {
                new ApiResource("api1", "My API")
            };
    }

    /// 
    /// 客户端应用
    /// 
    /// 
    public static IEnumerable GetClients()
    {
        return new List
            {
                new Client
                {
                    // 客户端ID 这个很重要
                    ClientId = "client",
                    //AccessToken 过期时间,默认3600秒,注意这里直接设置5秒过期是不管用的,解决方案继续看下面 API资源添加JWT
                    //AccessTokenLifetime=5, 
                    // 没有交互性用户,使用 clientid/secret 实现认证。
                    AllowedGrantTypes = GrantTypes.ClientCredentials, 
                    // 用于认证的密码
                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },
                    // 客户端有权访问的范围(Scopes)
                    AllowedScopes = { "api1" }
                },
                /// 资源所有者密码凭证(ResourceOwnerPassword)
                ///     Resource Owner其实就是User,所以可以直译为用户名密码模式。
                ///     密码模式相较于客户端凭证模式,多了一个参与者,就是User。
                ///     通过User的用户名和密码向Identity Server申请访问令牌。
                new Client
                {
                    ClientId = "client1",
                    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                    ClientSecrets = { new Secret("secret".Sha256()) },
                    AllowedScopes = { "api1" }
                }

            };
    }
}
补充说明(二):Startup.cs代码更替
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace WFinancial
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?linkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents = true;
                options.Events.RaiseSuccessEvents = true;
            })
                .AddDeveloperSigningCredential()//解决Keyset is missing 错误
                                                //.AddTestUsers(TestUsers.Users)
                                                //.AddInMemoryIdentityResources(IdpConfig.GetApiResources())
                .AddInMemoryApiResources(IdpConfig.GetApis())
                .AddInMemoryClients(IdpConfig.GetClients())
                .AddTestUsers(IdpConfig.Users);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();
            app.UseIdentityServer();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/286014.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号