Author : MD TAREQ HASSAN | Updated : 2021/04/18
Login with Azure AD
See: Azure AD as OpenID Connect Provider
Login With Google
- Dependency: 
Install-Package Microsoft.AspNetCore.Authentication.Google - Links:
    
- https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins
 - https://console.developers.google.com/cloud-resource-manager
 - https://console.developers.google.com/projectselector2
 - https://developers.google.com/identity/sign-in/web/sign-in#before_you_begin
 - Google auth in blazor app: https://www.freecodecamp.org/news/how-to-implement-google-authentication-and-authorization-in-server-side-blazor-app/
 
 - FYI, credentials used in creating demo app (screenshots) will be deleted (don’t try to use it, LoL)
 
User secrets (Secret manager)
{
  "Authentication:Google": {
    "ClientId": "475096194844-l2j1b79g863nvk4584eo8nerbfh59jrs.apps.googleusercontent.com",
    "ClientSecret": "UtgK_ArtjbP8mLc8_6xKJ9Oj"
  }
}
Settings in Startup.ConfigureServices()
services.AddAuthentication().AddGoogle(options =>
{
	IConfigurationSection googleAuthNSection =
		Configuration.GetSection("Authentication:Google");
	options.ClientId = googleAuthNSection["ClientId"];
	options.ClientSecret = googleAuthNSection["ClientSecret"];
});
Create a Google API Console project and client ID
Scaffold project and add Google login
Login With Facebook
https://jakeydocs.readthedocs.io/en/latest/security/authentication/sociallogins.html
Login With IdentityServer4
Resources: IdentityServer4 Login in dev-handy-sites#identity-server
Online temporary IS4 for testing: https://demo.identityserver.io/
Locally running IdentityServer4 project
- Prerequisite: IdentityServer4 project with in-memory store
 - This is for demo purpose only & we are gonna run both IdentityServer4 project and MVC Client app in same solution
    
- Right click on Solution > Properties > Startup Project > Multiple Start Up projects
 - Change ‘None’ to ‘Start’
 
 - You might get error:
    
- Error loading external login information.
 System.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it- https://stackoverflow.com/questions/50992544/identityserver4-with-asp-net-core-2-1-identity-error-loading-external-login-in
 
 
Create MVC Client App
- The whole idea of relying on Azure AD for user authentication is to avoid user management in your application
 - Add MVC Client App to the solution
 - Find Port no (need to configure cleint in IdentityServer4 project)
    
- Open: 
Properties/launchSettings.json "sslPort": xxx=>RedirectUris = http://localhost:xxx/signin-oidc
 - Open: 
 - See: http://docs.identityserver.io/en/3.1.0/topics/clients.html
 
Configure cleint (MVC Client App) in IdentityServer4 project
Config.cs
// ... ... ...
public static IEnumerable<Client> Clients =>
	new Client[]
	{
	  new Client
		{
			ClientId = "Foo_Client",
			ClientName = "Foo Client",
			AllowedGrantTypes = GrantTypes.Code,
			RequirePkce = true,
			ClientSecrets = { new Secret("hovermind.foo".Sha256()) },
			RedirectUris = { "http://localhost:xxx/signin-oidc" },
			FrontChannelLogoutUri = "http://localhost:xxx/signout-oidc",
			PostLogoutRedirectUris = { "http://localhost:xxx/signout-callback-oidc" },
			AllowOfflineAccess = true,
			AllowedScopes = { IdentityServerConstants.StandardScopes.OpenId, IdentityServerConstants.StandardScopes.Profile },
			AlwaysIncludeUserClaimsInIdToken = true,
		},
	};
	
// ... ... ...
Notes:
- End points (
signin-oidc,signout-oidc,signout-callback-oidc) will be handled by OpenID Middgleware of MVC Client App 
Install nuget package to MVC Client App
Package Manager Console > Select MVC Client App
Install-Package Microsoft.AspNetCore.Authentication.OpenIdConnect
Add user secrets to FooClient WebApp
Right click on FooClient WebApp > ‘Manage User Secrets’
secrets.json
{
  "IDP_IS4": {
    "ClientId": "Foo_Client",
    "ClientSecret": "hovermind.foo"
  }
}
Notes:
- IDP: Identity Provider
 - IS4: IndentityServer4
 
Get client ID and Secret uing IConfiguration
public class Startup
{
    public IConfiguration Configuration { get; }
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public void ConfigureServices(IServiceCollection services)
    {
		var clientId = Configuration["IDP_IS4:ClientId"];
		var clientSecret = Configuration["IDP_IS4:ClientSecret"];
		
        // ... ... ...
    }
    public void Configure(IApplicationBuilder app)
    {
        // ... ... ...
    }
}
Use OpenIdConnect Middleware
Startup.cs
public class Startup
{
    public IConfiguration Configuration { get; }
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
	public void ConfigureServices(IServiceCollection services)
	{
		
		// get client id and secret from user secrets
		var clientId = Configuration["IDP_IS4:ClientId"];
		var clientSecret = Configuration["IDP_IS4:ClientSecret"];
	
	
		services.AddControllersWithViews();
		//JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
		//JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
		services.AddAuthentication(options =>
		{
			options.DefaultScheme = "Cookies";
			options.DefaultChallengeScheme = "oidc";
		})
			.AddCookie("Cookies")
			.AddOpenIdConnect("oidc", options =>
			{
				options.Authority = "https://localhost:5001";
				options.ClientId = clientId;
				options.ClientSecret = clientSecret;
				options.ResponseType = "code";
				options.SaveTokens = true;
				options.Scope.Add(IdentityServerConstants.StandardScopes.Profile);
				options.Scope.Add(IdentityServerConstants.StandardScopes.OpenId);
				options.Scope.Add(IdentityServerConstants.StandardScopes.OfflineAccess);
			});
			
            services.AddAuthorization();
			
			//... ... ...
	}
	public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
	{
		if (env.IsDevelopment())
		{
			app.UseDeveloperExceptionPage();
		}
		else
		{
			app.UseExceptionHandler("/Home/Error");
		}
		app.UseStaticFiles();
		app.UseRouting();
		
		app.UseAuthentication();
		app.UseAuthorization();
		app.UseEndpoints(endpoints =>
		{
			endpoints.MapDefaultControllerRoute().RequireAuthorization();
		});
	}
}
Now, Start Multiple projects
More Demos:
- https://github.com/IdentityServer/IdentityServer4/tree/main/samples/Clients/src/MvcCode
 - https://github.com/IdentityServer/IdentityServer4/tree/main/samples/Quickstarts/3_AspNetCoreAndApis/src/MvcClient
 - https://github.com/IdentityServer/IdentityServer4/tree/main/samples/Quickstarts/2_InteractiveAspNetCore/src/MvcClient
 
More OpenID Connect Providers
- Others (list of providers: Github, Amazon, Apple, LinkedIn, ….): https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
 - Okta: https://developer.okta.com/blog/2019/11/15/aspnet-core-3-mvc-secure-authentication