首页 文章

将ASP.NET Core Identity设置为服务

提问于
浏览
2

我非常年轻的MVC应用程序

我想创建一个Web服务,为外部MVC应用程序提供身份 .

将ASP.NET Core Identity公开为服务的好处:

  • 实际的应用程序代码更简单,并且与其身份问题分离

  • 支持已 Build 的身份验证标准和模式,可简化安全问题并 Build 信任

  • 身份服务可以存在于单独的进程中

  • 在多个应用中重复使用用户身份

那可能吗 ?我能做到的任何想法吗?

1 回答

  • 1

    这个有可能 .

    这是我们的做法 .

    我们使用IdentityServer4为客户端生成JWT令牌 . 我们创建了一个简单的MVC项目,该项目具有以下简单的启动文件,可以为您提供一个想法 .

    public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentityServer()
            .AddSigningCredential(new X509Certificate2(Path.Combine(".", "cert", "token-cert.pfx"), "cert-password"))
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddClientStore<CustomClientStore>();
    
            string connectionString = Configuration.GetConnectionString("DefaultConnection");
            // a data service to fetch user data from database
            services.AddTransient<IUserDataMapper>(s => new UserDataMapper(connectionString));
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();
            loggerFactory.AddDebug();
    
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
    
            app.UseIdentityServer();
    
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("ACME Auth Token API v1.0");
            });
        }
    

    您可以在https://identityserver4.readthedocs.io/en/release/quickstarts/1_client_credentials.html#defining-the-api找到IdentityServer4的详细说明 .

相关问题