Google Authentication in AspNetCore.Identity

Starting to play with AspNetCore.Identity, but cannot complete a simple example, always get this exception:

An unhandled error occurred while processing the request. InvalidOperationException: The authentication handler is not configured to process the scheme: google

Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        // EF services
        services.AddEntityFramework()
            .AddEntityFrameworkSqlServer()
            .AddDbContext<MyContext>();

        // Identity services
        services.AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<MyContext>()
            .AddDefaultTokenProviders();

        // MVC services
        services.AddMvc().AddJsonOptions(options => {
            options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            options.SerializerSettings.Converters = new JsonConverter[] { new StringEnumConverter(), new IsoDateTimeConverter() };
        });

Configure.cs

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseIdentity();
        app.UseCookieAuthentication();
        app.UseGoogleAuthentication(new GoogleOptions()
        {
            ClientId = "xxx",
            ClientSecret = "xxx",
            AutomaticChallenge = true,
            AutomaticAuthenticate = true
        });

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

AuthController.cs

    [HttpGet]
    [AllowAnonymous]
    [Route("ExternalLogin", Name = "ExternalLogin")]
    public IActionResult ExternalLogin(string provider, string returnUrl = null)
    {
        var redirectUrl = Url.Action("ExternalLoginCallback", "Auth", new { ReturnUrl = returnUrl });
        var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
        return new ChallengeResult(provider, properties);
    }

An exception occurred somewhere after the return ChallengeResult. Did I miss something?

+4
source share
1 answer

app.UseIdentity() AutomaticAuthenticate = true google true. Identity cookie auth AutomaticAuthenticate, , undefined.

, .

+4

Source: https://habr.com/ru/post/1651079/


All Articles