【问题标题】:Facebook Authentication .NET CoreFacebook 身份验证 .NET Core
【发布时间】:2018-12-17 09:12:04
【问题描述】:

我对 Web 开发还很陌生,所以我一直在尝试使用 Microsoft 通过其默认 .NET Core 应用程序选项提供的模板代码。我能够使用 Azure 服务成功部署网站并将其连接到具有自己数据库的服务器。

然而,我一直在努力解决的一件事是设置 Facebook 身份验证。我遵循了这个指南:https://www.c-sharpcorner.com/article/authentication-using-facebook-in-asp-net-core-2-0/ 并且能够在本地成功登录 Facebook,但是当我尝试使用 ConfigureServices 函数中的新代码发布并访问网站时,我收到 HTTP 500 错误。

我的网站是:vincecoreapp.azurewebsites.net

这是我插入的sn-p:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

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


            services.AddAuthentication().AddFacebook(facebookOptions =>
            {
                facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
                facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
            });


            // Add application services.
            services.AddTransient<IEmailSender, EmailSender>();

            services.AddMvc();
        }

任何建议都会非常有帮助,谢谢!

【问题讨论】:

  • 500 错误是没有意义的。您需要获取正在引发的实际异常,包括堆栈跟踪。使用日志记录,或者,如果此站点尚未上线,您可以暂时将 ASPNETCORE_ENVIRONMENT 设置为“开发”,以允许显示开发人员异常页面。

标签: c# asp.net-core


【解决方案1】:

我的猜测是您尚未将已发布的“something.azurewebsites.net”URL 添加到您的Facebook App Settings 中的有效 OAuth 重定向设置中

【讨论】:

  • 问题是,当我包含该 sn-p 代码时,我什至无法访问我的网站。当我删除它时,一切都部署得很好,所以我不知道它为什么会导致 HTTP 500 错误。网站链接是 vincecoreapp.azurewebsites.net。
  • 啊,在这种情况下,我猜它缺少代码所需的东西,例如配置值或定义 AddFacebook() 方法的 DLL。找出问题的最佳方法是启用日志记录以获取实际错误。您可以按照上面评论中 Chris 的建议进行操作,或者尝试使用vincecoreapp.scm.azurewebsites.net/api/logstream 的日志流。最好使用 CURL 获取日志流,但我发现它至少在大部分时间都倾向于在浏览器中工作。
【解决方案2】:

命名空间 VinceCoreApp { 公共类启动 { 公共启动(IConfiguration 配置) { 配置=配置; }

    public IConfiguration Configuration { get; }

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

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


        services.AddAuthentication().AddFacebook(facebookOptions =>
        {
            facebookOptions.AppId = "680347028977338";
            //Configuration["Authentication:Facebook:AppId"];
            facebookOptions.AppSecret = "fc943b8782d7d5d83a4446709f2d903a";
            //Configuration["Authentication:Facebook:AppSecret"];
        });


        // Add application services.
        services.AddTransient<IEmailSender, EmailSender>();

        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();

        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseAuthentication();

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

}

这解决了问题

【讨论】:

    猜你喜欢
    • 2020-10-28
    • 2020-10-15
    • 2017-06-26
    • 2020-03-09
    • 2020-02-24
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 2021-06-15
    相关资源
    最近更新 更多