我有asp.net 2.0核心的解决方案,它包含以下项目:ASP.NET核心Web API授权属性返回404错误和重定向力
- 数据:一个类库,用于EF代码
- 的OAuth:如对于IdentityServer4代码
- 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();
然后授权属性按预期方式工作,但另一个新的问题,它显示500内部服务器错误,每当我运ened喜欢的AccountController任何控制器,它期待的UserManager的UserManager其构造
出现InvalidOperationException:无法解析 服务类型Microsoft.AspNetCore.Identity.UserManager`1 [MCare.Data.Entities.User] 试图激活MCare.Api.Controllers.AccountsController
我想知道为什么发生这种情况,如何解决这个问题,而不与API项目混合IdentityServer4项目,因为我已经看到了所有项目,他们在混合两者一个项目。
为什么在RestAPI项目中需要AspNetIdentity? – regnauld
创建帐户,重置密码和其他帐户管理功能 –
请从您开始在浏览器中请求授权路线的那一刻起发布您的日志。 – poke