【发布时间】:2018-12-04 23:53:12
【问题描述】:
我正在构建一个 .net 核心应用程序,我的 IdentityServer4 和一个 MVC 客户端运行正常。
在我的 IdentityServer 启动中,我可以成功添加 Asp.Net 身份,并且我可以访问本机 UserManager 和我的 UserContext(它扩展了 IdentityDbContext)。
我还有一个 API,我的 MVC 客户端(或其他客户端)可以调用它来获取有关登录用户的信息。这个 API 成功地依赖于 Identity Server,并且可以验证它接收到的 access_token、提取 sud、打开 UserContext(在通用程序集中定义)、找到用户并读取我添加到架构中的任何自定义表。不幸的是,我无法让 API 注册 Asp.Net 身份,因此无法使用 UserManager 来查看角色和其他本机表。
这是 IdentityServer 启动:
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<UserContext>()
.AddDefaultTokenProviders();
services.AddMvc();
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddAspNetIdentity<ApplicationUser>()
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(IdentityConnectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(IdentityConnectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
});
}
使用此 API 启动,请求已成功处理:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<UserContext>(options =>
options.UseSqlServer(UserDbConnectionString));
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
services
.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ApiName = "api1";
});
}
GET 请求的控制台输出(提供令牌)如下
信息:Microsoft.AspNetCore.Hosting.Internal.WebHost[1] 请求开始 HTTP/1.1 GET http://localhost:5001/authorisation
信息:Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2] 已成功验证令牌。
信息:Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[8] AuthenticationScheme:BearerIdentityServerAuthenticationJwt 已成功通过身份验证。
信息:IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationHandler[8] AuthenticationScheme: Bearer 认证成功。
信息:Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] 用户授权成功:Joe Bloke。
如果我像这样在 API 的启动中添加 Asp.Net Identity:
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<UserContext>()
.AddDefaultTokenProviders();
然后查询失败,日志显示用户为“null”。
信息:Microsoft.AspNetCore.Hosting.Internal.WebHost[1] 请求开始 HTTP/1.1 GET http://localhost:5001/authorisation
信息:Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2] 用户授权失败:(null)。
信息:Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3] 过滤器“Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter”处的请求授权失败。
这会导致重定向到 API 上不存在的登录页面,因此会出现 404。 我尝试在 API 中添加对 .AddAspNetIdentity() 的调用,但在 AddIdentityServerAuthentication 之后似乎无法识别它
是否可以让 API 访问 Asp.Net Identity UserManager? 我需要怎么做才能修复此身份验证错误?
谢谢
【问题讨论】:
-
你在使用 app.UseAuthentication();在 Configure(IApplicationBuilder app) 方法中。
-
是的,我是。但我认为这会从 IdentityServer4 服务中获取绑定,而不是从我的 asp.net 服务中获取?
-
@NDUF 我有一个类似的用例,您可以在哪里找到解决方案?
-
嗨,我没有。这个项目暂时搁置了,但无论如何我的计划略有改变:IdentityServer 可以直接访问 AspIdentity UserManager,但不能直接访问 API。相反,API 将使用我添加的一些自定义表,我在其中手动管理它们的角色。这是模型的一部分,因此任何有权访问模型的项目都可以看到这些表
标签: c# asp.net-core identityserver4