Create MVC 4 App With Google OAuth Provider

In this article, we will look into integration of the OAuth Google provider for authentication within MVC 4 applications. In MVC 4, we can authenticate a user with his Facebook, Google, Live or Twitter account and so on instead of registering with our website. Providing this feature within a website will have significant advantages, because users do not need to register and remember a new set of credentials. Let's create a sample Internet MVC 4 application and name it GoogleAuthSample. Go to: "App_Start\AuthConfig.cs" and uncomment the following line of code:

OAuthWebSecurity.RegisterGoogleClient();

MVC-1.jpg

We have integrated Google OAuth with the application. Run the application, there will a button added automatically in the Login page as shown below:

MVC-2.jpg

Clicking on the Google button will redirect to Google's login page to enter our credentials. After successful authentication, it will redirect to the following page, click on Accept:

MVC-3.jpg

It will redirect to a page where we can register the email address:

MVC-4.jpg

When we click on Register, it will store the user's details in Defaultconnection in the web.config:

MVC-5.jpg

Let's look at the database structure storing user's details in Server Explorer:

MVC-6.jpg

Once a user registers with his Google credentials, he can sign in with it into our website. Let's customize the Google button on the login page with the following code:
Go to "View\Account\ _ExternalLoginsListPartial.cshtml" and add the following code:

MVC-7.jpg

The Login page looks such as shown below:

MVC-8.jpg

We can customize the UI of External providers (Twitter, Facebook, Google and so on) by modifying following views under the Account folder:

MVC-9.jpg

I will end here with the previosu explanation and explain OAuth providers in future articles.

Up Next
    Ebook Download
    View all
    Learn
    View all