Overview
RealtimeChat supports Google OAuth 2.0 for social authentication, allowing users to sign in with their Google accounts. This guide walks you through setting up Google OAuth credentials and integrating them into your application.Google OAuth provides a secure way for users to authenticate without creating a separate password, improving user experience and security.
Prerequisites
Before you begin, ensure you have:- A Google account
- Access to Google Cloud Console
- RealtimeChat source code cloned locally
- Basic understanding of OAuth 2.0 flow
Step-by-Step Setup
Create Google Cloud Project
Navigate to Google Cloud Console and create a new project:
- Click “Select a project” dropdown in the top navigation
- Click “NEW PROJECT”
- Enter project name (e.g., “RealtimeChat”)
- Click “CREATE”
Enable Google+ API
Enable the Google+ API for your project:
- In the left sidebar, go to APIs & Services > Library
- Search for “Google+ API”
- Click on it and click ENABLE
The Google+ API is required for retrieving user profile information during OAuth authentication.
Configure OAuth Consent Screen
Set up the OAuth consent screen:
- Go to APIs & Services > OAuth consent screen
- Select External user type (unless you have Google Workspace)
- Click CREATE
- Fill in required fields:
- App name: RealtimeChat
- User support email: Your email
- Developer contact information: Your email
- Click SAVE AND CONTINUE
- Skip “Scopes” section (default scopes are sufficient)
- Add test users if needed for development
- Click SAVE AND CONTINUE
Create OAuth 2.0 Credentials
Create OAuth 2.0 Client ID credentials:
- Go to APIs & Services > Credentials
- Click CREATE CREDENTIALS > OAuth client ID
- Select Web application as application type
- Enter a name (e.g., “RealtimeChat Web Client”)
- Configure redirect URIs (see next step)
Configure Redirect URIs
Add the following authorized redirect URIs:Development:Production:Click CREATE to generate your credentials.
Save Client ID and Secret
After creating credentials, you’ll see:
- Client ID - A long string ending in
.apps.googleusercontent.com - Client Secret - A secret key for server-side authentication
Add Credentials to Configuration
Add your Google OAuth credentials to your application configuration.Or in Docker:
Development (appsettings.Development.json)
Create or editappsettings.Development.json:Applications/RealtimeChat.API/appsettings.Development.json
Production (Environment Variables)
For production, use environment variables or Azure Key Vault:docker-compose.yml
ASP.NET Core uses
__ (double underscore) to represent nested configuration in environment variables.Verify Configuration
Ensure your authentication configuration is loaded correctly:The application reads configuration from:
Applications/RealtimeChat.API/Extensions/AuthExtensions.cs
appsettings.json(base configuration)appsettings.{Environment}.json(environment-specific)- Environment variables (highest priority)
Testing OAuth Flow
Start the Application
http://localhost:5000 (or your configured port).
Test Login Flow
Authenticate with Google
You’ll be redirected to Google’s authentication page. Sign in with your Google account and grant permissions.
Callback Processing
After successful authentication, Google redirects back to:The
ExternalAuthService processes the callback:Applications/RealtimeChat.API/Services/ExternalAuthService.cs
OAuth Flow Details
Authentication Flow
The OAuth authentication flow in RealtimeChat works as follows:User Claims Extracted
During OAuth authentication, the following claims are extracted from Google:User’s email address (used as username)
Unique Google user identifier (provider key)
User’s full name (split into FirstName and LastName)
Troubleshooting
Redirect URI Mismatch
Error:redirect_uri_mismatch
Solution: Ensure the redirect URI in Google Cloud Console exactly matches the one configured in your application:
- Code:
/signin-google(seeAuthExtensions.cs:25) - Console: Must include full URL with port for development
Missing Configuration
Error:Value cannot be null. (Parameter 'ClientId')
Solution: Verify your configuration is loaded:
User Creation Fails
Error: User creation fails during OAuth callback Solution: Check database connection and ensure migrations are applied:Security Considerations
Development
- Use
appsettings.Development.json(add to.gitignore) - Or use .NET user secrets:
dotnet user-secrets set "Authentication:Google:ClientId" "your-client-id"
Production
- Use environment variables
- Use Azure Key Vault or AWS Secrets Manager
- Use managed identities where possible
- Rotate secrets regularly
OAuth Security
- Validate state parameter - Prevents CSRF attacks (handled by ASP.NET automatically)
- Use HTTPS in production - Required for secure cookie transmission
- Restrict redirect URIs - Only whitelist your application domains
- Non-persistent cookies - Sessions expire when browser closes (
isPersistent: false)
Next Steps
Authentication Overview
Learn about the overall authentication system
Identity Configuration
Explore ASP.NET Identity database configuration