【发布时间】: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