续IDS4建立Authorization erver和Client

匿名 (未验证) 提交于 2019-12-02 23:43:01

一、准备

创建一个名为QuickstartIdentityServer的ASP.NET Core Web 空项目(asp.net core 2.2),端口5000
创建一个名为Api的ASP.NET Core Web Api 项目(asp.net core 2.2),端口5001

二、定义服务端配置

1、在QuickstartIdentityServer项目中添加一个Config.cs文件:

using IdentityServer4.Models; using IdentityServer4.Test; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks;  namespace QuickstartIdentityServer {     public static class Config     {         public static IEnumerable<IdentityResource> GetIdentityResources()         {             return new IdentityResource[]             {                 new IdentityResources.OpenId()             };         }          public static IEnumerable<ApiResource> ApiResources()         {             return new[]             {                 new ApiResource("socialnetwork", "社交网络")             };         }         public static IEnumerable<Client> Clients()         {             return new[]             {                 new Client                 {                     ClientId = "socialnetwork",                     ClientSecrets = new [] { new Secret("secret".Sha256()) },                     AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,                     AllowedScopes = new [] { "socialnetwork" }                 }             };         }         public static IEnumerable<TestUser> Users()         {             return new[]             {                 new TestUser                 {                     SubjectId = "1",                     Username = "mail@qq.com",                     Password = "password"                 }             };         }     } }

2、

    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)         {             var builder = services.AddIdentityServer()            .AddDeveloperSigningCredential()            .AddInMemoryIdentityResources(Config.GetIdentityResources())            .AddInMemoryApiResources(Config.ApiResources())            .AddInMemoryClients(Config.Clients());             // rest omitted         }         // 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.Run(async (context) =>             {                 await context.Response.WriteAsync("Hello World!");             });         }     }

三、定义Api端配置

    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.AddAuthentication("Bearer")                 .AddJwtBearer("Bearer", options =>                 {                     options.Authority = "http://localhost:5000";                     options.RequireHttpsMetadata = false;                      options.Audience = "api1";                 });             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);         }         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.         public void Configure(IApplicationBuilder app, IHostingEnvironment env)         {             app.UseAuthentication();             if (env.IsDevelopment())             {                 app.UseDeveloperExceptionPage();             }              app.UseMvc();         }     }

2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!