-1

I have a asp.net C# web application. This app i created was using AD authentication and was accessible only within the organizations network. Because of the whole COVID thing i need to move it into a cloud service so the users can access. My plan was to migrate from AD into google as the organization utilizes google services. I was able to incorporate google sign in per the instructions here. But it doesnt get the email, and i need the email of the logged on user to use as a parameter on several sql queries. Apparently in 2019 google moved access of this info over to their "people" scope?? All the examples i can find are for asp.net MVC and CORE! I am just using regular ASP.net!!!! Any suggestions greatly appreciated!!

user2666821
  • 9
  • 1
  • 2

1 Answers1

0

I found an answer that was relatively simple to answer. Using the example set in Google to call the javascript from the front-end you can call getBasicProfile(). From here you can obtain the email as well as other info. We can set the values into a hidden field. From there we can access it in the back-end and do whatever..

 <div class="g-signin2" data-onsuccess="onSignIn" runat="server"  data-theme="dark"></div>



<asp:Button ID="btnProceed" CssClass="btn btn-primary" Enabled="false" runat="server" OnClick="Button1_Click" Text="Proceed" />


<script type="text/javascript">
    function onSignIn(googleUser) {
        document.getElementById("con").style.display = 'block';  
    // Useful data for your client-side scripts:
    var profile = googleUser.getBasicProfile();
    console.log("ID: " + profile.getId()); 
    console.log('Full Name: ' + profile.getName());
    console.log('Given Name: ' + profile.getGivenName());
    console.log('Family Name: ' + profile.getFamilyName());
    console.log("Image URL: " + profile.getImageUrl());
    console.log("Email: " + profile.getEmail());

    // The ID token you can pass to your backend:
      var id_token = googleUser.getAuthResponse().id_token;
      console.log("ID Token: " + id_token);
      document.getElementById('<%=hEmail.ClientID%>').value = profile.getEmail();  //HiddenField Controls
      document.getElementById('<%=hName.ClientID%>').value = profile.getName(); //HiddenField Controls
      document.getElementById('<%=Gtoken.ClientID%>').value = id_token; //HiddenField Controls

  }
</script>


<asp:HiddenField ID="hEmail" runat="server" />
        <asp:HiddenField ID="hName" runat="server" />
        <asp:HiddenField ID="Gtoken" runat="server" />

Server Backend

protected void Button1_Click(object sender, EventArgs e)
    {

        var hidEm = hEmail.Value;
        var hidName = hName.Value;

        Session["email"] = hidEm;
        Session["fName"] = hidName;

    }
user2666821
  • 9
  • 1
  • 2