Monday, November 19, 2018

Integrate Google (Gmail) Authentication in Asp.Net MVC 5.0 - OAuth 2.0

TODAY I AM GOING TO SHOW HOW TO AUTHENTICATION GOOGLE IN .NET MVC

STEP ONE:-
CREATE API IN GOOGLE API CLICK HERE


HTML CODE
Take a Button in HTML and give a id Like this

    <button type="button"  id="googleplus"> Login With Google</button>

   I Declare Here a Id name=(googleplus)


JAVA SCRIPT CODE

USE THIS SOURCE FILE FOR GOOGLE API

<script src="~/admin_assets/js/jquery-1.10.2.min.js"></script>
<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 type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.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: '530xxxxxxxxxxxxxxx-jh8losll6vk05f1ts8ir0ie2qlr7fpsm.apps.googleusercontent.com',
                    secret_key: 'Vci2wXJauQR9KjfYYwz00n06',
                    cookiepolicy: 'single_host_origin',
                });
                attachSignin(document.getElementById('googleplus'));
            });
    };
    function attachSignin(element) {
        auth2.attachClickHandler(element, {},
            function (googleUser) {
            $(".se-pre-con").show();
            console.log(googleUser);
                var profiles = googleUser.getBasicProfile();
                var ReturnUrl = 'http://localhost:55239/Account/ReturnURL';

            //Crate A Bunch Of Object for Search
            var objData = {
                Email: profiles.getEmail(),
                GoogleID: profiles.getId(),
                ProfileURL: profiles.getName(),
            };
            $.ajax({
                type: "POST",
                url: '@Url.Action("ReturnURL", "Account")', here put url like that
                data: objData,
                datatype: "json",
                success: function (data) {
                    if (data == 1) {
                        window.location = '/Account/CreateAccount';
                    }
                    else {
                        alert('This User Name Already Register  Try Anthor Option!');
                    }
                }
            });
                $(".se-pre-con").hide();
            });
    }
</script>
<script>startAppA();</script>



C# CODE
   [HttpPost]
   public JsonResult ReturnURL(string Email, string GoogleID, string ProfileURL, Registration_tblModel R)
        {
            Property p = new Property();
            p.Condition1 = GoogleID;
            p.OnTable = "FetechRegbyid";
            Ds = Dl.FetchMaster(p);
            Property info = new JustCallOnline.Models.Property();
            if (p.Condition1 != null)
            {
                if (Ds.Tables[0].Rows.Count > 0)
                {
  info.UserName = Ds.Tables[0].Rows[0]["FullName"].ToString(); HERE I CHECK IS USER ALREADY  REGIS OR NOT
                }
                else
                {
                    R.UserType = "USER";         HERE I PASS THE IN PROPERTY FILED FOR INSERT INTO DATABASE
                    R.FullName = ProfileURL;   
                    R.Emailid = Email;
                    R.Password = GoogleID;
                    R.AccountType = "Vendor";
                    try
                    {
       if (rdev.IU_SPROC<int>("IN_UP_REGISTRATION_TBL", R) > 0) HERE I CALL THE PROCEDURE FOR INSERT
                        {
                            TempData["MSG"] = "Account Created Mail Sent To your Email Id! Check Your Mail Id Wait For Admin Verification";
                            //Login Code
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    return Json(1, JsonRequestBehavior.AllowGet);

                }
            }
            return Json(0, JsonRequestBehavior.AllowGet);

        }



I THINK THIS ARTICAL HELPFULLY FOR ALL.IF ANY ISSUE PLEASE COMMENT ME. 



No comments:

Post a Comment

1.How to sum two table value in sql? Ans:- SELECT ( SELECT SUM ( London ) FROM CASH ) + ( SELECT SUM ( London ) FROM CHEQUE ) a...