• asp.net core的空Web项目集成相关dll和页面文件配置IdnetityServer4认证服务器

  • Ids4集成Identity

  • Ids4配置持久化到数据库

写在最前面,此文章不详细讲述IdentityServer4的各个组成部分以及Oauth2相关知识,需要了解详细相关知识的,可以移步我的其他几篇博客(初探IdentityServer4),腾讯视频有Dave老师录制的详细教程(http://v.qq.com/vplus/4cfb00af75c16eb8d198c58fb86eb4dc?page=video)。

asp.net core的空Web项目集成相关dll和页面文件配置IdnetityServer4认证服务器

  1. 首先创建一个net core版本为2.2的空项目,如下图所示。引入IdntityServer4和Identity的相关Nuget包 IdentityServer4,IdentityServer4.AspNetIdentity,IdentityServer4.EntityFramework,Microsoft.EntityFrameworkCore.Sqlite(数据库我们用Sqlite)。入下图所示IdentityServer4认证服务器集成Identity&配置持久化数据库IdentityServer4认证服务器集成Identity&配置持久化数据库
  2. 添加静态文件(wwwroot)和IdentityServer4的登录UI以及控制器相关类(官方文档的Quickstart),添加一个IdentityResource,ApiResource,和Client配置的Config类;因为Quickstart中用到的User类是继承自IdnetityUser的ApplicationUser,所以我们添加一个ApplicationUser类;项目路径是这样的:IdentityServer4认证服务器集成Identity&配置持久化数据库
  3. 接下来我们配置startup文件,这样,基于内存配置的(Config文件)我们的IdentityServer4认证服务器就搭好了
     1     public class Startup
     2     {
     3         public IConfiguration Configuration { get; }
     4         public IHostingEnvironment Environment { get; }
     5         public Startup(IConfiguration configuration, IHostingEnvironment environment)
     6         {
     7             Configuration = configuration;
     8             Environment = environment;
     9         }
    10         public void ConfigureServices(IServiceCollection services)
    11         {
    12             services.AddMvcCore()
    13            .AddAuthorization()
    14            .AddJsonFormatters();
    15 
    16             services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1);
    17 
    18 
    19 
    20             services.Configure<IISOptions>(iis =>
    21             {
    22                 iis.AuthenticationDisplayName = "Windows";
    23                 iis.AutomaticAuthentication = false;
    24             });
    25 
    26            
    27             var builder = services.AddIdentityServer(options =>
    28             {
    29                 options.Events.RaiseErrorEvents = true;
    30                 options.Events.RaiseInformationEvents = true;
    31                 options.Events.RaiseFailureEvents = true;
    32                 options.Events.RaiseSuccessEvents = true;
    33             })
    34                 .AddInMemoryIdentityResources(Config.GetIdentityResources())
    35                 .AddInMemoryApiResources(Config.GetApis())
    36                 .AddInMemoryClients(Config.GetClients())
    37                 .AddTestUsers(Config.GetUsers());
    38 
    39             if (Environment.IsDevelopment())
    40             {
    41                 builder.AddDeveloperSigningCredential();
    42             }
    43             else
    44             {
    45                 throw new Exception("need to configure key material");
    46             }
    47         }
    48         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    49         {
    50             if (Environment.IsDevelopment())
    51             {
    52                 app.UseDeveloperExceptionPage();
    53                 app.UseDatabaseErrorPage();
    54             }
    55             else
    56             {
    57                 app.UseExceptionHandler("/Home/Error");
    58             }
    59             app.UseStaticFiles();
    60             app.UseMvcWithDefaultRoute();
    61         }
    62            
    63         }
    64     }
    View Code

相关文章:

  • 2021-07-06
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2021-10-05
  • 2022-12-23
  • 2021-07-13
  • 2022-01-13
猜你喜欢
  • 2021-11-19
  • 2021-10-10
  • 2021-07-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案