Let’s first establish what the purpose of the code is, in the first place.
For this article, the purpose of the code is how to Sign in / Login with Google Plus in MVC application.
,
Step 1
In the first step, you need to create an empty MVC application.
- On the File menu, click New Project.
- In the New Project dialog box, under Project types, expand Visual C#, and then click Web. In the Name box, type "DemoGPlusAPI", then click on OK.
- Now, in the dialog box, click on "MVC" under ASP.NET 4.5.2 Templates, Then, click on "Change Authentication" which stays on the center-right side. Then, select "No Authentication".
Step 2
For Sign in / Login with Google Plus, first of all, we need to create an app in our Google account & then we need client_id and secret_key of that app. So, here we go for creating an app in Google account.
- Log in Google Developer Console for creating an app.
- Click on "Library" which lies in left side navigation or click on "Enable API" which stays at the top navigation.

- Select "Google+ API" under Social APIs.

- Click on "Enable" on top navigation.

- Select "Go To Credentials" on right side.

- Select "Web browser (JavaScript)" for "Determines" which settings you'll need to configure. Select "User Data" for the data that you would be accessing. Then, click on "What credentials do I need?"

- Finally, it's time to create an app for OAuth 2.0 client ID. Give your preferred app name. Provide your Origin URL & Redirect URL, then click on "Create client ID".

Redirect URL
It means that after executing the request, trigger will redirect to this URL.
Origin URL
The URL which contains the request. For Origin URL, right click on your project "DemoGPlusAPI" and select "Web". Then, copy your Project URL.
- Now, click on "Download" so that you can get your Client ID and Client Secret.

Step 3
Now, its time for implementation.
- Create an ActionMethod as below.
- public ActionResult Index() {
- return View();
- }
- Paste the below code in your View.
- @ {
- ViewBag.Title = "Google Plus API";
- } < div class = "row" > < h2 > Google + Login / Sign in < /h2> < br / > < button class = "btn btn-danger"
- id = "googleplus" > Google + API < /button> < br / > < /div>
- @section Scripts { < link href = "https://fonts.googleapis.com/css?family=Roboto"
- rel = "stylesheet"
- type = "text/css" > < script src = "https://apis.google.com/js/api:client.js" > < /script> < script >
- var googleUser = {};
- var startAppA = function() {
- gapi.load('auth2', function() {
- //Retrieve the singleton for the GoogleAuth library and set up the client.
- auth2 = gapi.auth2.init({
- client_id: '1047328192927-jch54k8ecj884h0ahfr685b9e10u23kd.apps.googleusercontent.com',
- secret_key: 'UCtG15hTOnTKzsPKsI8rMQ0X',
- cookiepolicy: 'single_host_origin',
- });
- attachSignin(document.getElementById('googleplus'));
- });
- };
- function attachSignin(element) {
- auth2.attachClickHandler(element, {}, function(googleUser) {
- debugger;
- $(".se-pre-con").show();
- console.log(googleUser);
- var profiles = googleUser.getBasicProfile();
- var ReturnUrl = 'http://localhost:58066/Home/ReturnURL';
- //Crate A Bunch Of Object for Search
- var objData = {
- Email: profiles.getEmail(),
- LastName: profiles.getFamilyName(),
- FirstName: profiles.getGivenName(),
- GoogleID: profiles.getId(),
- ProfileURL: profiles.getName(),
- };
- $.ajax({
- type: "POST",
- url: '@Url.Action("ReturnURL", "Home")',
- data: objData,
- datatype: "json",
- success: function(data) {
- alert("Successfully Done");
- }
- });
- $(".se-pre-con").hide();
- }, function(error) {
- alert(JSON.stringify(error, #ff0000, 2));
- });
- } < /script> < script > startAppA(); < /script>
- }
- Create a JsonResult Method.
- [HttpPost]
- public JsonResult ReturnURL(string Email, string FirstName, string LastName, string GoogleID, string ProfileURL) {
- //Do your code for Signin or Signup
- return Json(true, JsonRequestBehavior.AllowGet);
- }

Ankit KanojiaPosted Mar 12, 2020, 2:16 AM
Thank you so much Gaurang Mistry
Ankit KanojiaPosted Mar 12, 2020, 2:15 AM
Thank you so much Rushabh Patel
Ankit KanojiaPosted Mar 12, 2020, 2:15 AM
Thank you so much ahmed khattab
Ankit KanojiaPosted Mar 12, 2020, 2:15 AM
Thank you so much Amodu Bashir
Ankit KanojiaPosted Mar 12, 2020, 2:15 AM
Thank you so much eslam akrm
Ankit KanojiaPosted Mar 12, 2020, 2:14 AM
Thank you so much manav pandya
eslam akrmPosted Mar 2, 2019, 11:05 PM
Nice work sir, but I want to get the name of login user and I want to know that the user is login or not ?
Amodu BashirPosted May 27, 2018, 12:21 AM
Nice work sir...... please have you done anything on facebook login?
Amodu BashirPosted May 27, 2018, 12:20 AM
Nice work .....please have you actually done anything on facebook login
ahmed khattabPosted Jun 29, 2017, 2:07 PM
I got this errorThe following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "Scripts".Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Gaurang MistryPosted Nov 29, 2016, 1:43 AM
Good Demo bro. Really very helpful.
Rushabh PatelPosted Nov 29, 2016, 1:43 AM
Good work Suchit Khunt I need same thing for microsoft login. Please share if you can
Ankit KanojiaPosted Nov 29, 2016, 1:37 AM
Hii Manav Pandya.. Currently i am working on that.. in short time you get that all..
Manav PandyaPosted Nov 26, 2016, 1:05 AM
If possible i need Login demo with Linkedin and Facebook posting sir
Manav PandyaPosted Nov 26, 2016, 1:02 AM
I will try and lets se what happened
Manav PandyaPosted Nov 26, 2016, 1:02 AM
Thanks for sharing sir @Suchit Khunt