首页 技术 正文
技术 2022年11月19日
0 收藏 580 点赞 4,351 浏览 1801 个字

源码下载

这里根据《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

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

拿到token后 去访问ClientCredentialApi

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

修改token看看

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292