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看看
相关文章推荐
- 【ASP.NET Core分布式项目实战】(一)IdentityServer4登录中心、oauth密码模式identity server4实现
- 【ASP.NET Core分布式项目实战】(二)oauth2 + oidc 实现 server部分
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
- ASP.NET Core的身份认证框架IdentityServer4(8)- 使用密码认证方式控制API访问
- ASP.NET Core实现OAuth2.0的AuthorizationCode模式
- IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习保护API
- IdentityServer4 中文文档 -14- (快速入门)使用 ASP.NET Core Identity
- IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习保护API
- ASP.NET Core的身份认证框架IdentityServer4(1)-特性一览
- Elasticsearch,Kibana,Logstash,NLog实现ASP.NET Core 分布式日志系统
- 基于 IdentityServer3 实现 OAuth 2.0 授权服务【密码模式(Resource Owner Password Credentials)】
- ASP.NET Core:部署项目到Ubuntu Server
- ASP.NET Core的身份认证框架IdentityServer4(4)- 支持的规范
- ASP.NET Core的身份认证框架IdentityServer4(5)- 包和构建
- IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习保护API
- 【无私分享:ASP.NET CORE 项目实战(第二章)】添加EF上下文对象,添加接口、实现类以及无处不在的依赖注入(DI)
- [转]【无私分享:ASP.NET CORE 项目实战(第十四章)】图形验证码的实现
- 详解ASP.NET Core部署项目到Ubuntu Server
- ASP.NET Core实现OAuth2.0的AuthorizationCode模式
- ASP.NET Core实现类库项目读取配置文件