您的位置:首页 > 编程语言 > ASP

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

2018-01-13 11:02 1136 查看

源码下载

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

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐