【发布时间】:2019-08-02 03:58:28
【问题描述】:
我有一个 ASP.NET Identity Server 4 配置为使用 ASP.NET Core Identity (Auth Server) 进行授权和身份验证。我还有一个单独的 ASP.NET Core API (API) 配置为使用 Auth Server。从控制台应用程序中,我可以使用GrantTypes.ClientCredentials 对Auth Server 进行身份验证,并使用访问令牌执行对API 的请求。一切都按预期工作。
我想将此API 用作身份管理 API 来添加/编辑/删除用户、角色等,因此我使用 ASP.NET Core 身份对其进行配置,但现在当我从控制台执行相同的请求时应用程序,我得到一个 404,因为 API 正在重定向到不存在的登录屏幕(毕竟它是一个 API)。这告诉我的是当前的 AccessToken 和 Identity Server 身份验证没有被使用。这意味着我的新配置似乎覆盖了身份服务器。
API 启动.cs
public void ConfigureServices(IServiceCollection services)
{
string connectionString = Configuration.GetConnectionString("IdentityContextConnection");
//This was added for ASP.NET Identity
services.AddDbContext<IdentityContext>(options =>
options.UseSqlServer(connectionString));
//This was added for ASP.NET Identity
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<IdentityContext>()
.AddDefaultTokenProviders();
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
options.Authority = "http://localhost:5000";
if (Environment.IsDevelopment())
options.RequireHttpsMetadata = false;
options.Audience = "management-api";
});
}
// 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.UseAuthentication();
app.UseMvc();
}
如何让这个 API 针对我的 Auth Server 进行身份验证和授权,同时使用 ASP.NET 身份?
【问题讨论】:
标签: asp.net-core identityserver4 asp.net-core-2.2 asp.net-core-identity