Asp.Net MVC 6 身份 3 MongoDB 外部登录 (Facebook)

Asp.Net MVC 6 Identity 3 MongoDB External Login (Facebook)

我是 运行 ASP.NET MVC 6 的 RC1,我想使用 MongoDB 身份提供者。

我已经实现了 Grant Megrabyan 的 provider,它在注册新用户和允许他们登录方面做得很好,但我收到错误:

InvalidOperationException: No authentication handler is configured to handle the scheme: Microsoft.AspNet.Identity.External Microsoft.AspNet.Http.Authentication.Internal.DefaultAuthenticationManager.d__13.MoveNext()

我以前使用 EntityFramework 进行过外部登录,所以我假设我的第三方身份验证配置可能是正确的。

当用户点击使用 Facebook 登录时,他们将被重定向到以下操作:

    // POST: /Account/ExternalLogin
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public IActionResult ExternalLogin(string provider, string returnUrl = null)
    {
        // Request a redirect to the external login provider.
        var redirectUrl = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl });
        var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
        return new ChallengeResult(provider, properties);
    }

此时没有异常抛出,但是当来自 ChallengeResponse 的 Facebook returns 发送 GET 到:http://localhost:51265/signin-facebook?code=[facebook 用户令牌]。

此时ASP.NET抛出异常:

Facebook 的回调url 似乎没有意义。当然应该 return 到我的 ExternalLoginCallback 操作?

我就到这里没主意了?!

如果有人能看到我哪里出错了,那我会很高兴的。

我的startup.cs:

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        // Set up configuration sources.
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

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

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

    public IConfigurationRoot Configuration { get; set; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<ApplicationDbContext>();

        // Add framework services.
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddMongoStores<ApplicationDbContext, ApplicationUser, IdentityRole>()
            .AddDefaultTokenProviders();

        services.AddMvc();

        // 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.UseBrowserLink();
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());

        app.UseStaticFiles();

        app.UseFacebookAuthentication(options =>
        {
            options.AppId = "removed";
            options.AppSecret = "removed";
        });

        app.UseIdentity();

        app.UseMvcWithDefaultRoute();
    }

    // Entry point for the application.
    public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}

编辑:

我认为问题出在定义配置的顺序上。尝试先添加身份,然后再添加 Facebook 身份验证。

** 建议示例 **

app.UseIdentity();

app.UseMvcWithDefaultRoute();

app.UseFacebookAuthentication(options =>
    {
        options.AppId = "removed";
        options.AppSecret = "removed";
    });

UseIdentity 之后但在 UseMvc 之前调用 UseFacebookAuthentication

app.UseIdentity();

app.UseFacebookAuthentication(options =>
{
    options.AppId = "removed";
    options.AppSecret = "removed";
});

app.UseMvcWithDefaultRoute();