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

3

我正在运行ASP.NET MVC 6的RC1版本,并希望使用MongoDB身份验证提供程序。

我已经实现了Grant Megrabyan的提供程序,它很好地注册了新用户并允许他们登录,但我遇到了以下错误:

InvalidOperationException:未配置身份验证处理程序以处理方案: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);
    }

在这一点上没有抛出任何异常,但是当Facebook从ChallengeResponse返回时,它会发送一个GET到: http://localhost:51265/signin-facebook?code=[facebook user token]。
此时,ASP.NET会抛出异常:

enter image description here

Facebook生成的回调URL似乎没有意义。它应该返回到我的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);
}

请问您能否发布您的startup.cs文件吗?或者您可以自己检查一下:您是如何在其中配置Facebook身份验证的呢? - vzayko
感谢查看,startup.cs已添加。@vzayko - GavKilbride
2个回答

5
在使用UseIdentity之后,在UseMvc之前调用UseFacebookAuthentication方法;
app.UseIdentity();

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

app.UseMvcWithDefaultRoute();

在重构实体框架后,我还搞砸了我的账户控制器。谢谢你的帮助。 - GavKilbride

-1

编辑:

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

**建议示例**

app.UseIdentity();

app.UseMvcWithDefaultRoute();

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

1
尽管顺序很重要,但您建议的顺序意味着在配置身份验证之后才会配置Facebook身份验证。然后忽略了Facebook身份验证配置。 - GavKilbride

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接