Asp.net核心,外部登录,没有配置身份验证处理程序来处理该方案:Facebook

时间:2017-07-19 17:44:36

标签: c# asp.net facebook external identity

  

InvalidOperationException:没有配置身份验证处理程序来处理该方案:Facebook

[HttpGet]
    [AllowAnonymous]
    public IActionResult ExternalLogin(string provider, string returnUrl = "CampAccount")
    {
        // Request a redirect to the external login provider.
        var redirectUrl = Url.Action(nameof(ExternalLoginCallback), "Account", new { ReturnUrl = returnUrl });
        var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
        return Challenge(properties, provider);//Error occurs here
    }

如果有人遇到同样的问题,我会非常感谢你的想法,或者对我做错了什么做了建议。我正在使用(IdentityServer4)。

Startup.cs

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        if (env.IsDevelopment())
        {
            // For more details on using the user secret store see https://go.microsoft.com/fwlink/?LinkID=532709
            builder.AddUserSecrets<Startup>();
        }

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddDbContext<CampionzDBContext>(options =>
           options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        // if using IdentityServer4

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddMvc();

        // Adds a default in-memory implementation of IDistributedCache.
        services.AddDistributedMemoryCache();
        services.AddSession();
        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            ExpireTimeSpan = TimeSpan.FromMinutes(60)
        });

        app.UseStaticFiles();

        app.UseIdentity();

        // Add external authentication middleware below. To configure them please see https://go.microsoft.com/fwlink/?LinkID=532715

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


        //External login Secrets
        app.UseFacebookAuthentication(new FacebookOptions
        {
            AppId = "###",// Configuration["Authentication:Facebook:ClientID"],
            AppSecret = "###",//Configuration["Authentication:Facebook:ClientSecret"],
        });

        app.UseGoogleAuthentication(new GoogleOptions
        {
            ClientId = "###",// Configuration["Authentication:Facebook:ClientID"],
            ClientSecret = "###",//Configuration["Authentication:Facebook:ClientSecret"],
        });
    }
}

2 个答案:

答案 0 :(得分:1)

尝试在UseIdentity和UseMvc

之间放置外部身份验证中间件
app.UseIdentity();

之后放入外部中间件

app.UseFacebookAuthentication(new FacebookOptions
    {
        AppId = "###",// Configuration["Authentication:Facebook:ClientID"],
        AppSecret = "###",//Configuration["Authentication:Facebook:ClientSecret"],
    });

    app.UseGoogleAuthentication(new GoogleOptions
    {
        ClientId = "###",// Configuration["Authentication:Facebook:ClientID"],
        ClientSecret = "###",//Configuration["Authentication:Facebook:ClientSecret"],
    });

,最后一个块应为

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

答案 1 :(得分:0)

是的模式确实很重要。所以请在帖子及其下方的 上面 中使用正确的模式。

  1. app.UseIdentity();
  2. app.UseFacebookAuthentication(new FacebookOptions...);// Others Authenticater also will come here like Google,LinkedIn etc also will come here
  3. app.UseMvc(routes =>{...});
相关问题