ASP.NET Google and Facebook External Logins

Hamad Baloch Rakshani
Mr. Old School
Published in
3 min readJul 15, 2018
ASP.Net Microsoft documentation

Before I start boring the dear readers how to make External logins work smoothly when developing Web API 2. Let me give a shout out to .net community.

They are awesome, there is nothing you won’t find in this community. I for starters, wasn’t even a Web API and Database Architect 6 months ago. May the force be with my team lead in the company, who gave me an opportunity to learn. Ever since I moved to .Net I haven’t gone anywhere else. It is so vast and one can only do so much in it.

Well, that being said. A few days back I had a task to enable external logins for the a project. In my shallow attitude I thought it would be as simple as just registering creating an app on Facebook, and copy the app key and secret to my .net web API 2 project’s startup.auth.cs.

And to my much surprise, it didn’t work at all. The documentations on Official Microsoft website follow Single Page Applications. Which is different than Web API project.

I went through everything that I could find on first search page of Google. However, there was one page in particular that had the answers, and mind you. This was a community forum. God Bless the people for replying so much on the posts.

Don’t get me wrong, but stack overflow is too overrated. Most of the people are pretentious and make fun if someone asks a simple question. Hell, stackoverflow itself suggests users to flag the questions which are basic or don’ include code.

Implementing External Registration

Before you can implement External Registrations, you have to make sure you connect Facebook and Google with your code. For doing so, follow these links below.

  1. So, for Facebook, please follow this detailed documentation : Facebook ASP.Net Authentication

2. For Google : Google ASP.Net Authentication

Note: When you have completed creating projects and apps on Google and Facebook. Stop there, and follow my lead. Don’t Go further. Follow this now.

Make sure, you don’t set up .net core project. Select highlighted option in Figure 1.1

figure 1.1: Creating a .Net Project

Basically, what core require custom coding. Mostly from scratch. Whereas, .Net Framework is as simple as calling the function. In plain words .Net framework in Laravel for C#, only better *winks*.

Next step is to choose Web API and change authentication to Individual Users. Refer to Figure 1.2.

figure 1.2: setting templates for .Net Framework project

Now Copy the client ID and Client Secret From Google, and paste Facebook App key and secret in red circled snippet and Google id and secret into gray circled. Startup.Auth.cs file.

figure 1.3: authentication your project with facebook and google.

Now, what you have to do is simply go to AccountController.cs and paste the following code under RegisterExternal(RegisterExternalBindingModel model) function

private async Task<ExternalLoginInfo> AuthenticationManager_GetExternalLoginInfoAsync_WithExternalBearer(){ExternalLoginInfo loginInfo = null;var result = await Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ExternalBearer);if (result != null && result.Identity != null){var idClaim = result.Identity.FindFirst(ClaimTypes.NameIdentifier);if (idClaim != null){loginInfo = new ExternalLoginInfo(){DefaultUserName = result.Identity.Name == null ? "" : result.Identity.Name.Replace(" ", ""),Login = new UserLoginInfo(idClaim.Issuer, idClaim.Value)};}}return loginInfo;}

Once done. replace the red circled lined in Figure 1.4 with var info = await AuthenticationManager_GetExternalLoginInfoAsync_WithExternalBearer();

figure 1.4

And Viola! Your External Registration should work like a charm.

Kindly give a clap, if it helped and let me know if you face any difficulty in setting up.

Bless up!

CIAO!

--

--

Hamad Baloch Rakshani
Mr. Old School

Leads a pack of engineers 🐺 | Legend | Recently dabbled in Management and DevOps 💻 | Movie junkie & 2-books-a-year nerd.