Search code examples
c#asp.net-mvc-5nullframeworksgoogle-authentication

Google External Login in MVC 5 C# (.Net Framework)


I'm trying to add the Google External Login feature for one of my Web Applications using Visual Studio 2017 (MVC 5 .Net Framework). By following the exact steps given before, I'm not able to make it work. And after signing in with google account (previously assigned in test users), it always redirects to the login page. In debugging mode all the variables and properties are null.

Further, on my issue, I have tried each of the following steps (based on previous solutions from StackOverflow:-

  1. Updating all related NuGet packages to the latest version.

  2. Configuring Authorized Javascript and redirect URL based on my project (HTTP://localhost:{port number})

  3. The application is in Test Mode (Test users were added).

  4. The Code for Google authentication in (App_Start-> Startup.Auth.cs) looks something like this:

    app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

    app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
            {
    
                ClientId = "My Client ID Given in Google Developer Console",
    
                ClientSecret = "My Client Secret Given in Google Developer Console"
            });
    

I don't know if someone else has faced the same issue recently. And I tested this on Asp.Net Core project, and it works fine. But it does not work in MVC 5 Framework. Please advise if any.

Best


Solution

  • It seems the issue is related to HTTP , so you need to use HTTPs instead. Please, follow these steps:-

    • Right Click on your project name-> Properties.

    • In Properties window enable SSL.

    • Copy the entire SSL URL (the port number is different than HTTP).

    • paste your new SSL URL to Authorized Redirect URL in Google Developer Console. e.g

    • When you run your application in visual studio. Don't forget to change HTTP to HTTPs and change the port number before trying to singing via google.

    • The complete Authorized Redirect URL is as below:

    Authorized Redirect URL : https://localhost:{new port number}/signin-google

    enter image description here