【问题标题】:Using ASP.NET Core Identity with IdentityServer4 - Authentication使用 ASP.NET Core Identity 和 IdentityServer4 - 身份验证
【发布时间】:2020-08-27 15:52:14
【问题描述】:

我正在尝试了解如何在微服务环境中实现安全性,目前正在考虑使用 .NET Core Identity 进行用户访问管理(用户名、密码、哈希等)和 IdentityServer4 用于令牌的想法基于身份验证和管理。

这是因为我希望大量客户端进行身份验证:将使用用户名和密码的 Blazer 网站;其他可能使用令牌的内部 API;和一个移动应用程序,它也将使用 OAUTH 令牌/刷新令牌逻辑。

我正在尝试在一个微服务中实现所有这些,因此所有客户端都可以在一个地方进行身份验证 - 某种看门人。

我的问题是:这是个好主意还是我应该拆分服务?

其次:Wnen 我正在测试来自邮递员的登录 API 调用我收到 404,因为 API 试图将我重定向到登录页面。我希望这里出现 401,因为我已将身份验证方案定义为 Bearer

这是我的代码:

   public void ConfigureServices(IServiceCollection services)
        {
       services.AddControllers().AddNewtonsoftJson();
       var connectionString = Configuration.GetConnectionString("DefaultConnection");
                
       //add Users and Role system
        services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(connectionString));

            //this configures the dependancy injection for the UserManager in the Identity controller constructor.
        services.AddIdentity<IdentityUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();
    
       //add Client tokens system
      
       var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
            services.AddIdentityServer()
                .AddConfigurationStore(options =>
                {
                    options.ConfigureDbContext = builder =>
                        builder.UseSqlServer(connectionString,
                        sql => sql.MigrationsAssembly(migrationsAssembly));
                })
                .AddOperationalStore(options =>
                {
                    options.ConfigureDbContext = b => b.UseSqlServer(connectionString,
                        sql => sql.MigrationsAssembly(migrationsAssembly));
                })
               .AddAspNetIdentity<IdentityUser>();//required for Identity and IdentityServer4 to play nice together.
    
       //add authentication for this service
        services.AddAuthentication("Bearer")
           .AddIdentityServerAuthentication(options =>
           {
               options.Authority = "http://localhost:5001";//this service
               options.RequireHttpsMetadata = false;
               options.ApiName = "Identity";
           });
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //  InitializeIdentityServerDatabase(app);

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseIdentityServer();

            app.UseAuthorization();
            
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

【问题讨论】:

  • 你的 Startup.Configure 方法是什么样的?
  • 您应该阅读有关开发 IDP 的内容。使用 IDP,您希望将客户端重定向到您的登录页面或第 3 方登录页面,而不是将登录页面作为尝试进行身份验证的应用程序的一部分。
  • @GuyLowe 你解决过这个问题吗?我一直在为 identity/identityserver4 苦苦挣扎很久,我想我明白了,很想聊天。
  • 不,还没有。您可以在这里添加答案吗?

标签: c# asp.net-core asp.net-identity microservices identityserver4


【解决方案1】:

只有当您从授权中间件收到质询时,AddIdentityServerAuthentication 才会启动,即控制器/操作上有一个授权属性。

一般来说,我也总是建议您将 IdentityServer 与您的客户端和 API 分开,以便更好地分离关注点。

【讨论】:

  • 对,所以我应该有一个单独的 IdentityServer 微服务和另一个处理身份层的微服务?我认为通过组合它们,我可以共享 1 个资源和 1 个数据库,因为将它们全部放在一起是有意义的。我猜不是?
  • 你是如何向 API 端点(控制器?)添加授权的,我猜你会得到 404。通过将 IdentityServer 放在自己的服务中(可能与 ASP.NET Identity 一起)进行用户管理,那么您将来可以更轻松地添加更多共享同一 IdentityServer 的服务...... IdentitySever 最难的部分是如何管理用户。
  • 我从我的答案中删除了一些部分,因为它可能是正确的,我认为 404 和 401 的问题是由于授权,而不是管道设置。但我可能错了。
  • 如果你有一个纯 API,只包含 AddIdentityServerAuthentication,那么你不应该从你的 API 中看到任何 404 的页面,这些页面是使用 authorize 属性找到并保护的。正如我之前所说,您在同一个锅中有许多服务,因此很难推断谁在做什么。
  • 好主意!为了理智,把它们分开会让你的生活更轻松!我认为了解您构建的内容并将其放在同一个应用程序中很重要,因此很难对其进行推理。
猜你喜欢
  • 1970-01-01
  • 2018-12-30
  • 2022-06-13
  • 2020-04-15
  • 2018-07-06
  • 2021-05-28
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多