Working With External Providers in MVC 5

Introduction

This article explains customization of a social providers button in the ASP.NET MVC 5 using Visual Studio Studio 2013. As you all know, we can create the MVC 5 Project based ASP.NET Web Application in Visual Studio 2013, so here I am customizing the login providers buttons.

In that context, you need to be aware of the configuration of MVC 5 external login options.

Registration of OAuth Providers

We need to first enable the authentication providers for the project. Use the following procedure to create a sample of doing that.

Step 1: Open the Startup.Auth.cs file.

Startup Authentication File

Step 2: Enable the Google provider as shown below:

  1. app.UseGoogleAuthentication();  

Step 3: Run your application and open the Login page.

Login with Google in MVC 5

Step 4: Use the same and open other providers and enter the app_id and app_secret value as "x" as shown below:

 

  1. app.UseTwitterAuthentication(  
  2.    consumerKey: "x",  
  3.    consumerSecret: "x");  
  4.   
  5. app.UseFacebookAuthentication(  
  6.    appId: "x",  
  7.    appSecret: "x");  

 

These are the simple social provider buttons to be displayed on your login page.

All Providers in MVC 5

Customization of OAuth Providers

I am using a CSS file that is expressed on GitHub. Use the following procedure to do that.

Step 1: Create a CSS file in Content.

Creating Css File

Step 2: Name it "Zocial.css".

Enter Css

Step 3: Copy the content in it from the CSS file stored in GitHub.

Step 4: Open the Bundle Configuration file.

Bundle Configuration File

Step 5: Modify your code with the following code:

 

  1. bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(  
  2.           "~/Scripts/bootstrap.js",  
  3.           "~/Scripts/respond.js"));  
  4.   
  5. bundles.Add(new StyleBundle("~/Content/css").Include(  
  6.           "~/Content/bootstrap.css",  
  7.           "~/Content/site.css",  
  8.           "~/Content/zocial.css"));  

 

In the code above I added the reference of my CSS file created in the Content folder.

Step 6: Now, open the external login file as in the following:

External Login File

Step 7: Modify the code with the following code:

  1. using (Html.BeginForm(action, "Account"new { ReturnUrl = returnUrl }))  
  2. {  
  3.     @Html.AntiForgeryToken()  
  4.     <div id="socialLoginList">  
  5.         <p>  
  6.         @foreach (AuthenticationDescription p in loginProviders)  
  7.             {  
  8.                 <button type="submit"  
  9. class="zocial @p.AuthenticationType.ToLower()"  
  10. id="@p.AuthenticationType"  
  11. name="provider" value="@p.AuthenticationType"  
  12. title="Log in using your @p.Caption account">  
  13. @p.AuthenticationType</button>  
  14.             }  
  15.         </p>  
  16.     </div>  
  17.  

Step 8: Run your application and open the Login page.

All Customized Providers in MVC 5

Summary

This article will help you to customize the external authentication providers in your MVC 5 project using Visual Studio 2013. You can also create the MVC 5 project in Visual Studio 2012 with Web Tools 2013.1. Thanks for reading.


Similar Articles