- Go to Google Cloud Console
- Create OAuth 2.0 Client ID
- Add redirect URI:
http://localhost:5000/api/auth/google-callback - Copy Client ID and Client Secret
cd InventoryX.Presentation
dotnet user-secrets set "Authentication:Google:ClientId" "YOUR_CLIENT_ID"
dotnet user-secrets set "Authentication:Google:ClientSecret" "YOUR_CLIENT_SECRET"dotnet runFrom your SPA:
// Redirect to Google login
window.location.href = 'http://localhost:5000/api/auth/external-login?provider=Google&returnUrl=' +
encodeURIComponent(window.location.origin + '/auth/callback');Or test with Swagger:
- Navigate to
http://localhost:5000/swagger - Find
/api/auth/external-loginendpoint - Execute with:
{"provider": "Google", "returnUrl": "http://localhost:5173"}
POST /api/auth/login?useCookies=true
Content-Type: application/json
{
"email": "user@example.com",
"password": "Password123!"
}POST /api/auth/external-login
Content-Type: application/json
{
"provider": "Google",
"returnUrl": "http://localhost:5173/auth/callback"
}POST /api/auth/register
Content-Type: application/json
{
"email": "user@example.com",
"password": "Password123!",
"name": "John Doe"
}POST /api/auth/logoutGET /api/auth/pingauthSPA → POST /api/auth/login → Cookie Set → Authenticated
1. SPA → GET/POST /api/auth/external-login (with returnUrl parameter)
2. Backend → Redirect to Google OAuth
3. User → Authenticates with Google
4. Google → Redirects to Backend /api/auth/google-callback
5. Backend → Creates/Signs In User + Sets Cookie
6. Backend → Redirects to SPA returnUrl
7. SPA → Receives redirect (already authenticated with cookie)
✅ Dual Authentication: Password or Google OAuth
✅ Automatic User Creation: New users created on first Google login
✅ Email Auto-Confirmation: Google users don't need email verification
✅ Cookie-Based Auth: Secure, HttpOnly cookies for SPA
✅ CORS Configured: Ready for cross-origin requests
✅ OpenIddict Integration: Full OAuth 2.0 support
- ✅
IdentityApiExtensions.cs- Added Google OAuth endpoints - ✅
DependencyInjection.cs- Configured Google authentication - ✅
appsettings.json- Added Google credentials section - ✅
AppDbContext.cs- OpenIddict tables (already present)
- Set up Google credentials (see GOOGLE_OAUTH_SETUP.md)
- Implement SPA integration (see SPA_AUTH_EXAMPLE.tsx)
- Test the flow with your frontend
- Deploy to production with proper HTTPS and credentials
Problem: "Google ClientId not configured"
Solution: Set user secrets or update appsettings.json
Problem: CORS errors
Solution: Add your SPA origin to Frontend:AllowedOrigins in appsettings.json
Problem: Cookie not set
Solution: Ensure credentials: 'include' in fetch requests
Problem: Redirect URI mismatch
Solution: Verify callback URL in Google Console matches exactly: http://localhost:5000/api/auth/google-callback
Problem: Google redirects to frontend instead of backend
Solution: This has been fixed. The /external-login endpoint now properly configures Google to redirect to the backend /google-callback first, then the backend redirects to your frontend returnUrl
For detailed documentation, see:
GOOGLE_OAUTH_SETUP.md- Complete setup guideSPA_AUTH_EXAMPLE.tsx- React integration examples