InvalidOperationException:未指定authenticationScheme,并且未找到DefaultAuthenticateScheme

时间:2018-12-16 23:18:00

标签: asp.net oauth-2.0 openid-connect

我知道stackoverflow对此标题有疑问,但是我没有找到解决方法。

我的Startup.cs类

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddAuthentication().AddGoogle("Google",
            options =>
            {
                options.ClientId = "<my client id>";
                options.ClientSecret = "<my client secret>";
            }
        ).AddCookie();



        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

我的控制器:

public class AuthController : Controller
{
    public IActionResult Index()
    {
            return View();
    }

    [Authorize(AuthenticationSchemes = "Google")]
    public IActionResult Protected()
    {
        return View();
    }
}

我收到错误消息:

  

处理请求时发生未处理的异常。   InvalidOperationException:未指定authenticationScheme,并且   找不到DefaultAuthenticateScheme。   Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext   上下文,字符串方案)

能帮我吗?

我不想设置默认身份验证,我想添加使用多个提供程序进行登录的功能。

0 个答案:

没有答案