In this article I will explain with an example, how to login with Google in ASP.Net MVC.
In this article, ASPSnippets.GoogleAPI library will be used for login with Google in ASP.Net MVC.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 
Download ASPSnippets.GoogleAPI Library
You can download the latest ASPSnippets.GoogleAPI.dll from the following link.
Note: You will need to add the reference of ASPSnippets.GoogleAPI DLL in your project.
 
 
Getting Google API Client ID and Client Secret
In order to use Google Account API for login, you will need to create an Application in Google Console and get Client ID and Client Secret. For more details please refer the following article.
 
 
Model
JSON
Following JSON returned from Google API, it consists of some additional information which are not covered in this article and you can easily extend your Model class as per your requirement.
Login with Google in ASP.Net MVC
 
Model
The following Model class will be used to hold Google details.
public class GoogleProfileModel
{
    public string Id { getset; }
    public string Name { getset; }
    public string Picture { getset; }
    public string Email { getset; }
    public string Verified_Email { get; set; }
    public bool IsDisable { getset; }
}
 
 
Namespaces
You will need to import the following namespaces.
using System.Web.Script.Serialization;
using ASPSnippets.GoogleAPI;
 
 
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this action method, the ClientId, ClientSecret and the RedirectUri properties are set and an instance of GoogleProfileModel class is created.
Then, a check is performed to the Code returned from Google, if the Code is available, then using the Code, user’s Google profile details such as Google Profile ID, Name, Picture, Email and Verified Email status are fetched using the Fetch method of the GoogleConnect class.
Finally, the returned JSON string is deserialized to the GoogleProfileModel class object using the Deserialize method of JavaScriptSerializer class and returned to the View.
 
Action method for handling Login POST operation
This action method gets called when Login button is clicked.
Inside this Action method, page will be redirected to Google for inputting user details for authentication using the Authorize method of GoogleConnect class.
Then, request is sent to get access to the Profile and Email details of the user by passing the profile and email scopes to Authorize method respectively and finally, sent back to the URL set as the RedirectUri.
Note: You need to set valid Redirect Uri in the Google Console App otherwise you will get redirect mismatch error discussed later in the article.
 
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        GoogleConnect.ClientId = "<Google Client ID>";
        GoogleConnect.ClientSecret = "<Google Client Secret>";
        GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0];
 
        GoogleProfileModel profile = new GoogleProfileModel();
        string code = Request.QueryString["code"];
        if (!string.IsNullOrEmpty(code))
        {
            GoogleConnect connect = new GoogleConnect();
            string json = connect.Fetch("me", code);
            profile = new JavaScriptSerializer().Deserialize<GoogleProfileModel>(json);
            profile.IsDisable = true;
        }
 
        return View(profile);
    }
 
    [HttpPost]
    public ActionResult Login()
    {
        GoogleConnect.Authorize("profile", "email");
        return RedirectToAction("Index");
    }
}
 
 
View
Inside the View, in the very first line the GoogleProfileModel class is declared as Model for the View.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Login.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
 
The following Form consists of:
Button – For allowing user login with Google Account.
Image – For displaying Google Profile picture.
SPAN – For displaying the user’s Google Profile ID, Name, Email and Email Verification status.
 
Submitting Form
When the Button is clicked, the form is submitted to the Controller’s Login Action Method.
@model Google_SignIn_MVC.Models.GoogleProfileModel
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Login", "Home", FormMethod.Post))
    {
        <input type="submit" value="Login" name="Submit" disabled="@Model.IsDisable" />
        if (!string.IsNullOrEmpty(Model.Id))
        {
            <hr />
            <table>
                <tr>
                    <td rowspan="6" valign="top">
                        <img ID="imgProfile" src="@Model.Picture" Width="50" Height="50" />
                    </td>
                </tr>
                <tr>
                    <td>ID: <span>@Model.Id</span></td>
                </tr>
                <tr>
                    <td>Name: <span>@Model.Name</span></td>
                </tr>
                <tr>
                    <td>Email: <span>@Model.Email</span></td>
                </tr>
                <tr>
                    <td>Verified Email: <span>@Model.Verified_Email</span></td>
                </tr>
            </table>
        }
    }
</body>
</html>
 
 
Errors
Following are some errors that you might encounter when using the ASPSnippets.GoogleAPI in your projects.
1. invalid_client
Login with Google in ASP.Net MVC
 
The cause of this error is directly related to the Client ID and Client Secret and hence you need to make sure that you have supplied a valid Client ID and Client Secret.
 
2. invalid_request
Login with Google in ASP.Net MVC
 
This error will occur when the Client ID and/or Client Secret is blank or not supplied.
 
3. redirect_uri_mismatch
Login with Google in ASP.Net MVC
 
The above error will occur when the RedirectUri set in the code is not matching with the RedirectUri set while configuring the application in Google Developer Console.
 
4. deleted_client
Login with Google in ASP.Net MVC
 
The above error will occur when the Client ID is deleted from Google Developer Console.
 
 
Screenshot
Login with Google in ASP.Net MVC
 
 
Demo
 
 
Downloads