ASP.NET Core web application with OAuth Facebook provider error

Viewed 1500

I have a very simple web app consisting of the default ASP.NET Core web application to which I've added the Facebook OAuth provider and an Sqlite database.

Got it working locally without any trouble but when I deploy to Azure, I started getting errors.

After a while I got to an error stating:

An error occurred while starting the application 

After searching the internet for a couple of hours and trying various ways to actually see the error, I stumbled upon the answer here.

Now I could see the error details:

ArgumentException: The 'ClientId' option must be provided.
Microsoft.AspNetCore.Authentication.OAuth.OAuthMiddleware..ctor(RequestDelegate next, IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, UrlEncoder encoder, IOptions<SharedAuthenticationOptions> sharedOptions, IOptions<TOptions> options)
Microsoft.AspNetCore.Authentication.Facebook.FacebookMiddleware..ctor(RequestDelegate next, IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, UrlEncoder encoder, IOptions<SharedAuthenticationOptions> sharedOptions, IOptions<FacebookOptions> options)
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.Extensions.Internal.ActivatorUtilities+ConstructorMatcher.CreateInstance(IServiceProvider provider)
Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
Microsoft.AspNetCore.Builder.UseMiddlewareExtensions+<>c__DisplayClass3_0.<UseMiddleware>b__0(RequestDelegate next)
Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

This is weird because all the documentation I can find online says you should only need AppId and AppSecret for Facebook. I followed the Microsoft guide to get this configured. Here is the relevant bit of Startup.cs:

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

I've found examples showing ClientId being used for the Google and Microsoft providers but not Facebook. (it's a big page but just search for 'ClientId') I've tried adding ClientId but just get the same error.

4 Answers

You're getting this error because the key Authentication:Facebook:AppId can't be found in your Configuration object. On a typical ASP.NET Core application, that object is filled from the appsettings.json file.

You're saying that it works locally, but not on Azure, which probably means that you configured your Facebook auth in appsettings.Development.json, which is used on your Dev box but not when deploying to Azure.

So to sum up, your appsettings.json file should look like this:

{
  "Authentication": {
    "Facebook": {
      "Id": "1943349465943333",
      "AppSecret": "4bbc86c773aa1df91d52b421e129433"
    }
  }
}

You can see https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration for more information.

ERROR: ArgumentException: The 'AppId' option must be provided. (Parameter 'AppId') Microsoft.AspNetCore.Authentication.Facebook.FacebookOptions.Validate()

This worked for me in .net 6 facebook login error. Just removed configuration[] and that was it.

var builder = WebApplication.CreateBuilder(args);

var services = builder.Services;
var configuration = builder.Configuration;

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

Just remove the tag ... Configuration[

...and it should work

instead of this,

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

use this

app.UseFacebookAuthentication(new FacebookOptions()
{
    AppId ="Authentication:Facebook:AppId"],
    AppSecret = "Authentication:Facebook:AppSecret"]
});
Related