1

I am using ASP.NET Core 2.0 Web API to create application with authentication. I want to login using "login/password" combination and using Facebook. I am using JWT tokens for authorization. From Startup.cs I am calling extension method RegisterAuth.

public static void RegisterAuth(this IServiceCollection services, AuthSettings authSettings)
{
    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options =>
        {
            options.SaveToken = true;
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidateAudience = false,
                ValidateLifetime = true,
                ValidateIssuerSigningKey = false,
                ValidIssuer = authSettings.Issuer,
                ValidAudience = authSettings.Audience,
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authSettings.SecretKey))
            };
        })
        .AddFacebook(facebookOptions =>
        {
            facebookOptions.AppId = authSettings.Facebook.AppId;
            facebookOptions.AppSecret = authSettings.Facebook.AppSecret;
            facebookOptions.SignInScheme = "Bearer";
        });
}

In my controller I have 2 methods. Login for "login/password" combination which it works and returns me jwt token. And SignIn for facebook which does not work.

[Route("SignIn/{provider}")]
public IActionResult SignIn(string provider)
{
    return Challenge(new AuthenticationProperties(), provider);
}

SignIn redirects to the facebook page from where after signing in it throws an exception.

InvalidOperationException: No IAuthenticationSignInHandler is configured to handle sign in for the scheme: Bearer

So please help me to fix Facebook Auth. Thank you!

1 Answer 1

1

Just have to change Facebook SignInScheme and add cookies.

1) Change "Bearer"; to CookieAuthenticationDefaults.AuthenticationScheme; and add cookie.

2) After AddAuthentication add

.AddCookie()
.AddFacebook(facebookOptions =>
{
    facebookOptions.AppId = authSettings.Facebook.AppId;
    facebookOptions.AppSecret = authSettings.Facebook.AppSecret;
    facebookOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @gor-asatryan, I don't understand very well all authentication schemes, but I think in this case, you do not use JWT approach anymore but the Cookie one, no?
Yes. Using cookie authentication for facebook and jwt for login+password combination
How do you handle the Token/Cookie expiration use case with FB? or will it change only when the user Signed Out? or life time one?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.