ASP.NET核心Web API授权属性返回404错误和重定向力
我有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项目,因为我已经看到了所有项目,他们在混合两者一个项目。
当DefaultChallengeScheme遇到可能导致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;
});
是的,但我不希望重定向发生,因为它的Web Api和它没有登录页面,我希望从该控制器返回401 UnAuthorized状态代码。 –
在Core 2.0之前,有一个属性可以关闭AutoChallenge。在2.0 Ithink中,您需要通过在选项中的OnRedirectToLogin事件中添加自己的代码来覆盖AutoChallenge。让我看看我是否可以找到某个地方的文章... https://*.com/questions/45878166/asp-net-core-2-0-disable-automatic-challenge – Geekn
解决方案是显式设置默认身份验证并像你提到的那样挑战模式。谢谢。 –
为什么在RestAPI项目中需要AspNetIdentity? – regnauld
创建帐户,重置密码和其他帐户管理功能 –
请从您开始在浏览器中请求授权路线的那一刻起发布您的日志。 – poke