我有IIS安装程序,托管两个.NET核心应用程序。一个用于身份验证,另一个用于API。
根是spa应用程序,因此没有后端服务器代码。
这是我的配置:
验证项目:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("BasicBlock.Connection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddAuthentication(o =>
{
o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
services.ConfigureApplicationCookie(options =>
{
options.Cookie.Name = "foo.cookie";
options.Cookie.Path = @"/";
});
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddMvc();
}
这是API:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(o =>
{
o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
services.ConfigureApplicationCookie(options =>
{
options.Cookie.Name = "foo.cookie";
options.Cookie.Path = @"/";
});
services.AddMvc();
}
两者都调用app.UseAuthentication();在配置中。
调用带有authorize属性的api端点时出现以下错误。
InvalidOperationException:未指定authenticationScheme,也未找到DefaultChallengeScheme。