Google Authentication in ASP.NET Core MVC applications provides a secure and easy way for users to log in using their Google accounts. This guide walks you through the entire process of integrating Google Authentication into your ASP.NET Core MVC application.

Step 1. Create a new ASP.NET Core MVC project

  1. Open Visual Studio: Launch Visual Studio from your computer.
  2. Create a new project: Click on Create a new project from the start page.
  3. Select Project Template: In the "Create a new project" dialog, select ASP.NET Core Web Application, then click Next.
  4. Configure Project: Provide a Project name and Location for your project, then click Create.
  5. Select Application Type: In the "Create a new ASP.NET Core web application" dialog, choose Web Application (Model-View-Controller) and ensure .NET 5.0 or later is selected. Click Create.

Step 2. Configure the Google API console

  1. Access Google API Console: Navigate to the Google API Console.
  2. Create a New Project: Click on the project dropdown and select New Project. Fill in the project details and click Create.
  3. Configure the OAuth Consent Screen
    • Go to the OAuth consent screen.
    • Select External as the user type and fill in the required fields (App name, User support email, etc.). Click Save and Continue.
  4. Create OAuth Credentials
    • Go to Credentials.
    • Click Create Credentials and select OAuth 2.0 Client IDs.
    • Choose Web application as the application type.
    • Set the Authorized redirect URIs to https://localhost:{PORT}/signin-google (replace {PORT} with your port number, typically 5001 for HTTPS).
    • Click Create and copy the Client ID and Client Secret for later use.

Step 3. Add Google authentication to your ASP.NET Core application

Step 4. Update your MVC application for authentication

Step 5. Run your application

  1. Start the Application: Press F5 to build and run your application.
  2. Login with Google
    • Navigate to your application's homepage.
    • Click on the Login with Google link.
    • You will be redirected to Google's login page. Enter your credentials.
  3. Post-login Redirection: After successful login, you will be redirected back to your application, now authenticated.

Output

Now we will Hit the google login button the google panel will appear. You need to select the account and then authenticate it.

Google Panel

After successful login, we are on the home page.

Home Page

GitHub Project Link: https://github.com/SardarMudassarAliKhan/GoogleAuthInAspNetCoreMVC

Conclusion

By following these steps, you can successfully integrate Google Authentication into your ASP.NET Core MVC application. This setup allows users to securely log in using their Google accounts, enhancing both security and the user experience. Depending on your needs, you can further customize the authentication process, manage user profiles, and implement role-based access control.