我有一个asp.net核心2.0解决方案,它包含以下项目:
- 数据:EF代码的类库
- OAuth:作为IdentityServer4代码的web应用程序项目
- Api:为我的Api创建为空的web项目
现在OAuth项目配置了aspnetidentity,它运行良好,我可以在身份验证后获得令牌,这是它的启动代码:
public void ConfigureServices(IServiceCollection services)
{
// connect with normal database
services.AddDbContext<MCareContext>(options => options.UseSqlServer(Configuration
.GetConnectionString("MCareConnection")));
services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<MCareContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// IS configurations database
var dbConnectionString = Configuration.GetConnectionString("MCareConnection.OAuth");
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
services.AddIdentityServer()
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(dbConnectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(dbConnectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
})
.AddAspNetIdentity<User>()
.AddDeveloperSigningCredential();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
DatabaseInitializer.InitializeDatabase(app);
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseIdentityServer();
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}
现在API项目的问题是,每当我打开任何授权的控制器/操作时,它都会给我404错误,这是启动代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MCareContext>(options => options.UseSqlServer(Configuration
.GetConnectionString("MCareConnection")));
services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<MCareContext>()
.AddDefaultTokenProviders();
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:60415";
options.ApiName = "mCareApi";
options.RequireHttpsMetadata = false;
});
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
app.UseAuthentication();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}
如果我关闭上面代码上的这个部分:
//services.AddIdentity<User, IdentityRole>()
//.AddEntityFrameworkStores<MCareContext>()
//.AddDefaultTokenProviders();
然后Authorize属性按预期工作,但另一个新问题是,每当我打开任何像AccountController这样的控制器时,它都会显示500内部服务器错误,该控制器的构造函数上会出现UserManager UserManager
InvalidOperationException:无法解析类型的服务Microsoft.AspNetCore.Identity.UserManager`1[MCare.Data.Entitys.User]在尝试激活MCare.Api.Controllers.AccountsController 时
我想知道为什么会发生这种情况,以及如何在不将IdentityServer4项目与API项目混合的情况下解决这个问题,因为我看到的所有项目都将它们混合在一个项目中。
DefaultChallengeSchema是否可能在遇到可能导致404的authorize属性时重定向到不存在的页面,例如登录页面?
尝试将默认质询设置为Jwt模式,该模式返回未授权。
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:60415";
options.ApiName = "mCareApi";
options.RequireHttpsMetadata = false;
});
或者,您可以尝试我在下面文章中提到的方法,为事件提供一个处理程序。
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
options.JwtBearerEvents = new JwtBearerEvents
{
OnChallenge = context =>
{
context.Response.StatusCode = 401;
return Task.CompletedTask;
}
};
options.Authority = "http://localhost:60415";
options.ApiName = "mCareApi";
options.RequireHttpsMetadata = false;
});
也许您必须在AddControllers()行之后添加AddAuthentication行