Third-party authentication to an existing regular ASP.NET MVC project

Hi Folks, this post is regarding authentication implementation with third parties like Google, Facebook, etc. in ASP.NET MVC.

Challenge: To figure out what additional code changes visual studio does when ‘Individual User Accounts’ selected in ‘Change Authentication’ while creating a project.

Solution: This code changes obtained by comparing the difference between two projects – one created with default one ‘No Authentication’ and the other created with ‘Individual User Accounts’.

The intention of this post is to observe the code changes done for the ‘Individual User Accounts’. In case of an existing project with no authentication, one could set up the third party authentication by making code changes as per the differences observed in this post.

Indeed, one can install Identity NuGet packages but it won’t add source code for App_start, Controllers, Models, Views, and Startup.

Let’s see the difference.

ProjectDifference

Copy below to packages.config file and restore them. Or find them in NuGet manager and install them.

<package id="EntityFramework" version="6.2.0" targetFramework="net472" />
<package id="Microsoft.AspNet.Identity.Core" version="2.2.2" targetFramework="net472" />
<package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.2" targetFramework="net472" />
<package id="Microsoft.AspNet.Identity.Owin" version="2.2.2" targetFramework="net472" />
<package id="Microsoft.Owin" version="4.0.0" targetFramework="net472" />
<package id="Microsoft.Owin.Host.SystemWeb" version="4.0.0" targetFramework="net472" />
<package id="Microsoft.Owin.Security" version="4.0.0" targetFramework="net472" />
<package id="Microsoft.Owin.Security.Cookies" version="4.0.0" targetFramework="net472" />
<package id="Microsoft.Owin.Security.Facebook" version="4.0.0" targetFramework="net472" />
<package id="Microsoft.Owin.Security.Google" version="4.0.0" targetFramework="net472" />
<package id="Microsoft.Owin.Security.MicrosoftAccount" version="4.0.0" targetFramework="net472" />
<package id="Microsoft.Owin.Security.OAuth" version="4.0.0" targetFramework="net472" />
<package id="Microsoft.Owin.Security.Twitter" version="4.0.0" targetFramework="net472" />
<package id="Owin" version="1.0" targetFramework="net472" />

Update the targetFramework value as yours.

Add the new source code files to your project. Click here to download the new files. Copy them to respective folders. Remember to update the namespace with your namespace.

Now to enable third party authentication, for instance, Google. Go to Startup.Auth.cs, uncomment below code and provide client id and client secret.

//app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
//{
// ClientId = "",
// ClientSecret = ""
//});

To obtain values for above two keys please refer https://developers.google.com/adwords/api/docs/guides/authentication.

Similarly, you can do for other providers.

Build and run, click login link to open the login page. You could see ‘Google’ login link.

Hope this helps to understand the additional code and how to set up authentication in already existing project manually from scratch.

PS: If you find any other challenge or have any suggestion that could improve or correct this article, please feel free to drop a comment and I will try to improve this article.