.NET Core 2 Cookie身份验证。收到InvalidOperationException:没有authenticationScheme

时间:2018-07-16 14:20:11

标签: .net authentication .net-core

我有IIS安装程序,托管两个.NET核心应用程序。一个用于身份验证,另一个用于API。

  • / fooSPA-SPA应用
  • / fooSPA / API-API .NET Core 2
  • / fooSPA / Auth-Auth .NET Core 2

根是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。

enter image description here

0 个答案:

没有答案