我有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