locked
How to sign in with Facebook account in an ASP.NET MVC project? RRS feed

  • Question

  • User-1688616113 posted

    Hi!

    So I follow the official guide on creating an ASP.NET MVC project and enabling Facebook authentication, by Rick Anderson of Microsoft. I am able to authenticate with Facebook, but I still can't sign in. The problem seems to be that I do not get the registration form when I authenticate for the first time.

    1. I create a new ASP.NET MVC project.
    2. I enable individual user accounts.
    3. I update OWIN from 3.0.1 to 4.0.0.
    4. I add RequireHttps attribute to the Home controller.
    5. I enable SSL.
    6. I copy the HTTPS URL and paste it in as the project URL.
    7. I create a new Facebook app, add Facebook Login (OAuth) and configure it for my localhost app.
    8. I enable the UseFacebookAuthentication method and add my app ID and app secret.

    The problem seems to be that I do not get the registration form when I authenticate for the first time. So when I click on the Facebook sign in button, the URL changes from https://localhost:44333/Account/Login to https://localhost:44333/Account/Login#_=_ and no registration form is displayed.

    What am I missing?

    Updating OWIN

    Enabling SSL

    Adding API key and authenticating

    FYI

    If you are a frustrated ASP.NET beginner like myself, and you stumbled across this post in search for answers, please make note that the guide I linked to above is 2 years, 11 months and 20 days old at the time of this writing. It's amazing how fast things get outdated. One thing you need to do that's not mentioned in the guide is that you need to provide a valid redirect URI. I believe this is a new requirement for OAuth 2.0. If you fail to do that, you will run into the problem you see below when you try to authenticate.

    All you really need to do is add the local HTTPS address for your app as you see below.

    Saturday, February 24, 2018 4:09 PM

All replies

  • User-1688616113 posted

    This is not an answer to the question, but it clearly shows that something about Facebook login is broken. Because I can add Google login and it works just fine, as you can see from the images below.

    app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
    {
    ClientId = "blablabla.googleusercontent.com",
    ClientSecret = "blablabla"
    });

    So what should I do about Facebook login? Why is it not working? What's wrong with it? I have already updated OWIN to the latest version, I am using SSL, and I did add OAuth redirect URI accordingly. What else can I do?

    Why is Google login working but not Facebook login? Why am I not redirected to the ExternalLoginCallback? This appears to be an async method in the Account controller.

    Only after adding Google login does my AspNetUsers and AspNetUserLogins table get populated, after I fill out the form and register my new Google authenticated user.

    But why is the same not working with Facebook? What's the problem? I can repeat the same process any number of times, starting all over from scratch with a new ASP.NET MVC project, the outcome will be the same, Facebook login will not work. In complete contrast to ASP.NET Core Razer Pages project, where I am able to enable and login with Facebook, Google and Twitter. Is the ASP.NET MVC project template simply too old and outdated for use with Facebook login today? Should it not be enough to just update OWIN to the latest version and get it working with the latest Facebook API?

    For the record, I am using Visual Studio Community edition, 15.5.5 on Windows 10 Pro, v. 1709, .NET target 4.6.1.

    Saturday, February 24, 2018 6:19 PM
  • User-166373564 posted

    Hi, 

    Have you tried installing package

    Install-Package Microsoft.Owin.Security.Facebook

    from nuget package manager?

    The Microsoft.Owin.Security.Facebook namespace provides types related to Facebook authentication.

    Regards,

    Angie 

    Tuesday, February 27, 2018 6:21 AM
  • User-832373396 posted

    <g class="gr_ gr_42 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="42" data-gr-id="42">Hi</g> <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="5" data-gr-id="5">samirgunic</g>,

    Sir, before, I tested an working example with facebook account. and met the same error as yours 

     

    Solution

    please add more URLs to it,  and I think http://localhost:**/Account/Login is required beside http://localhost:**/.

    Here is what I did an working example

    With regards, Angelina Jolie

    Tuesday, February 27, 2018 8:43 AM
  • User1723109129 posted

    Dears

    I had the same exact issue and this is what I have done to make it work. In the Nuget Manager, check below packages.

    • Make sure OWIN > 1.0
    • Make sure Newtonsoft.JSON > 9.0
    • Upgrade Microsoft.OWIN to 4.0.0
    • Upgrade Microsoft.OWIN.Security to 4.0.0
    • Upgrade Microsoft.OWIN.Security.Facebook to 4.0.0

    Thanks, @Angie Xu - MSFT for the hint.

    BR, Moiyd

    Wednesday, January 23, 2019 6:46 AM