ASP.NET Core分布式项目-2.oauth密码模式identity server4实现

源码下载

这里根据《ASP.NET Core分布式项目-1.IdentityServer4登录中心》的代码来继续更新oauth密码模式,这里的密码模式比上次的客户端模式更安全

在WebApiIdentityServer服务端的config里添加用户

 public class config
    {
        //IdentityServer配置——用户
        //IdentityServer用户-这里通过提供一个简单的C#类完成,
        //当然你可以从任何数据存储加载用户。
        //我们提供了ASP.NET Identity 和MembershipReboot支持检索用户信息。
        public static IEnumerable<ApiResource> GetResources()
        {
            return new List<ApiResource> { new ApiResource("api","MQapi")};
        }

        //IdentityServer需要一些关于客户端信息,这可以简单地提供使用客户端对象
        public static IEnumerable<Client> GetClients()
        {
            return new List<Client>
            {
                new Client()
                {
                    ClientId="ClientId",
                    AllowedGrantTypes=GrantTypes.ClientCredentials,//客户端模式
                    ClientSecrets={ new Secret("secrt".Sha256())},
                    AllowedScopes={ "api"}
                },
                new Client()
                {
                    ClientId="pwdClient",
                    AllowedGrantTypes=GrantTypes.ResourceOwnerPassword,//密码模式
                     ClientSecrets={ new Secret("secrt".Sha256())},
                     RequireClientSecret=false,
                    AllowedScopes={ "api"}
                }
            };
        }

        //模拟用户
        public static List<TestUser> GetTsetUsers()
        {
            return new List<TestUser>{
                new TestUser{
                    SubjectId="1",
                    Username="MQ",
                    Password="123456"
                }
            };
        }
    }

  然后再去配置Startup

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //添加依赖注入配置
            services.AddIdentityServer()
                .AddDeveloperSigningCredential()
                .AddInMemoryApiResources(config.GetResources())
                .AddInMemoryClients(config.GetClients())
                .AddTestUsers(config.GetTsetUsers());
                
            services.AddMvc();
        }

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

  运行WebApiIdentityServer 和 ClientCredentialApi测试下 dotnet watch run

打开 paotman 

拿到token后 去访问ClientCredentialApi

 

 修改token看看

 

转载于:https://www.cnblogs.com/MingQiu/p/8278592.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring-security-oauth2-2.2.3.release.jar是一个用于实现基于OAuth 2.0协议的安全认证和授权的Java库。OAuth 2.0协议是一种用于授权的开放标准,它允许用户通过授权代理向第三方应用程序授权访问受保护的资源,而无需将用户的凭证(例如用户名和密码)透露给第三方。 spring-security-oauth2-2.2.3.release.jar提供了一套面向Spring应用程序的OAuth 2.0认证和授权解决方案。它可以轻松地将OAuth 2.0集成到现有的Spring应用程序中,并提供了一系列的API和类,使开发人员可以方便地实现OAuth 2.0认证和授权流程。 使用spring-security-oauth2-2.2.3.release.jar,开发人员可以通过配置和自定义一些核心组件(例如TokenStore、AuthorizationServer和ResourceServer)来实现OAuth 2.0的四种授权类型:授权码模式密码模式、客户端模式和简化模式。通过这些授权类型,开发人员可以实现不同的应用场景和需求。 此外,spring-security-oauth2-2.2.3.release.jar支持使用不同的存储机制来存储和管理OAuth 2.0令牌,例如内存存储、数据库存储和Redis存储。开发人员可以根据自己的需求选择并配置合适的存储机制。 总的来说,spring-security-oauth2-2.2.3.release.jar提供了一种简单、可扩展且安全的方式来实现OAuth 2.0认证和授权。无论您是开发面向Web、移动还是其他类型应用程序,该库都能帮助您轻松地实现与第三方应用程序的安全集成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值