Adding Google OAuth2 Login to ASP.NET MVC Web App (Local Development)
While working through the AZ-204 lab, I practiced integrating third-party authentication using Google OAuth2 into an ASP.NET MVC (.NET Framework) application. It gave me hands-on experience with secure external login flows. Below is the full step-by-step process I followed.
1. Project Setup in Visual Studio
-
Open Visual Studio 2022 and install:
- "ASP.NET and web development"
- ".NET Framework project and item templates"
- ".NET Framework 4.8 targeting pack"
-
Create a new project using the ASP.NET Web Application (.NET Framework) template.
-
Select the MVC template and set Authentication to Individual User Accounts.
-
This will scaffold Identity and all required auth logic automatically.
2. Test Local Authentication Flow
-
Press F5 to run the app.
-
Click Register, fill out a test email and password, then register.
-
Confirm the user is logged in and appears authenticated.
-
Log out and log back in to verify credentials are saved.
Note: User data is stored in a .mdf
file using SQL Server LocalDB under your AppData directory.
3. Enable SSL for OAuth2 Compatibility
-
In Solution Explorer, select your project and press F4.
-
Set SSL Enabled to True and copy the SSL URL (e.g.,
https://localhost:44356
). -
Go to Project Properties → Web tab → paste SSL URL into Project Url.
-
Save and close.
4. Register OAuth2 Client in Google Developer Console
-
Go to the Google API Console.
-
Create a new project.
-
Under OAuth consent screen, fill out required info (select “External” if prompted).
-
Go to Credentials → click Create OAuth client ID:
- Type: Web application
- Name:
OAuthGoogleAppClient
- Redirect URI:
https://localhost:<port>/signin-google
-
Copy the Client ID and Client Secret.
5. Configure ASP.NET for Google Login
-
Open
App_Start/Startup.Auth.cs
. -
Find the Google block (commented out by default):
//app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions() //{ // ClientId = "", // ClientSecret = "" //});
-
Uncomment it and insert your credentials:
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions() { ClientId = "<your-client-id>", ClientSecret = "<your-client-secret>" });
-
Save the file.
6. Test Google Login Integration
-
Press F5 to run the app.
-
Click Login → choose Google.
-
Sign in with your Google account and approve access.
-
You’ll be redirected to
/signin-google
→ then to Register (if it’s your first login). -
Confirm registration and verify login success.
Notes
- Your redirect URI must match exactly in both your app and Google Console.
- OAuth2 flows are handled via browser redirects (not server-side).
- New Google-linked users will be stored using ASP.NET Identity in LocalDB.
OAuth2 Flow Recap
1. User visits your site
2. Clicks "Login with Google"
3. Redirected to Google auth server
4. User signs in and consents
5. Google redirects back to /signin-google
6. ASP.NET logs them in using received token