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

标签 asp.net-mvc mongodb asp.net-identity asp.net-core-mvc asp.net-identity-3

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

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

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);
    }

此时没有抛出异常,但是当 Facebook 从 ChallengeResponse 返回时,它发送一个 GET 到:http://localhost:51265/signin-facebook?code=[facebook用户 token ]。

此时 ASP.NET 抛出异常:

enter image description here

Facebook做的callback 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);
}

最佳答案

UseIdentity之后、UseMvc之前调用UseFacebookAuthentication

app.UseIdentity();

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

app.UseMvcWithDefaultRoute();

关于asp.net-mvc - Asp.Net MVC 6 身份 3 MongoDB 外部登录 (Facebook),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34219341/

相关文章:

asp.net-mvc - asp.net mvc 和身份验证示例

c# - 如何在 EF 6 中对字符串类型的字段创建唯一约束?

c# - 使用 mongodb 和指南作为文档 ID 存储指南以轻松检索实际 Guid 的有效方法是什么?

c# - 没有 Entity Framework DbContext 的 OWIN 配置

asp.net - 临时向登录身份用户添加声明

asp.net-identity - 无法使用 Microsoft.Owin.Security.WsFederation 和 ADFS 3 登录

c# - RadioButtonFor 和标签

javascript - 防伪造 token 和 Ajax JSON.stringify Post 不起作用

sql - rails : mixing NOSQL & SQL Databases

javascript - 更新 MongoDB 文档中的属性类型和值?