asp.net - Pass front-end input value to code behind / google federated login -


i tried use google federated login function retrieve user info in order implement one-click registration functionality website.

i have retrieved information on client side. used several hidden input fields store values parameters name,email, etc. however, when accessed these fields code behind, i got empty values.

here steps took inspect problem: 1. have made sure each input field has [runat="server"] tag , have made sure values correctly returned google. 2. have made sure input fields inside form

i wondering if because did not submit form once clicked google+ login button, button following:

<div id="signin-button" class="slidingdiv">  <div class="g-signin" data-callback="loginfinishedcallback"   data-approvalprompt="force"   data-clientid="....."   data-scope="  https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me "   data-height="short"   data-cookiepolicy="single_host_origin"   > </div> </div> 

this button lead me other functions verify credentials , return parameters request. wondering how modify make form submitted once click on button?

if not caused issue, think of other possible reasons why not hidden input value code behind?

here callback function

 function loginfinishedcallback(authresult) {       if (authresult) {           if (authresult['error'] == undefined) {               gapi.auth.settoken(authresult);                toggleelement('signin-button'); /               getinfo();                document.getelementbyid('button').click();           } else {               console.log('an error occurred');           }       } else {           console.log('empty authresult');         }   } 

getinfo google plus information , , button.click() supposed retrieve information client side server side

the google+ sign-in button not post form data instead passes authorization code , access token page in callback function configured in button. might able clever use javascript update hidden field content (which might doing?) , use data hidden field oauth 2 code exchange on server side.

an alternate approach used in google+ c# quick start sample. sample use api endpoint post authorization code later exchanged server-side oauth 2 credentials.

i have thrown some code pass authorization code authorizing backend. consists of form frontend:

<%@ page language="c#" autoeventwireup="true" codebehind="fedlogin.aspx.cs"     inherits="gplusquickstartcsharp.fedlogin" %>  <!doctype html>  <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">     <title></title> </head> <body>     <script type="text/javascript">         var confirms = 0;         function loginfinishedcallback(result) {             console.log(result);             if (result.access_token != null) {               gapi.client.load('plus', 'v1', function(){                 gapi.client.load('oauth2', 'v2', function () {                   gapi.client.plus.people.get({ userid: 'me' }).execute(                     function (resp) {                         document.getelementbyid('name').value = resp.displayname;                         confirms += 1;                         if (confirms > 1) {                             document.getelementbyid('form1').submit();                         }                     });               });               gapi.client.load('oauth2', 'v2', function () {                 gapi.client.oauth2.userinfo.get().execute(                     function (resp) {                         document.getelementbyid('email').value = resp.email;                         confirms += 1;                         if (confirms > 1) {                             document.getelementbyid('form1').submit();                     }                     });                   });                 });                 document.getelementbyid('code').value = result.code;             }         }     </script>     <div id="signin-button" class="slidingdiv">         <div class="g-signin" data-callback="loginfinishedcallback"             data-clientid="....apps.googleusercontent.com"             data-scope="https://www.googleapis.com/auth/userinfo.email"             data-height="short"             data-cookiepolicy="single_host_origin"         >     </div>     </div>      <form id="form1" runat="server" method="post">     <div>         <input type="hidden" name="code" id="code" />         <input type="hidden" name="email" id="email" />         <input type="hidden" name="name" id="name" />     </div>     </form> </body> <script type="text/javascript">   (function () {     var po = document.createelement('script');     po.type = 'text/javascript'; po.async = true;     po.src = 'https://plus.google.com/js/client:plusone.js';     var s = document.getelementsbytagname('script')[0];     s.parentnode.insertbefore(po, s);   })(); </script> </html> 

and code behind [c#]:

using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols;  namespace gplusquickstartcsharp {     public partial class fedlogin : system.web.ui.page     {         protected void page_load(object sender, eventargs e)         {             if (request["code"] != null)             {                 // perform code exchange here                 var result = manualcodeexchanger.exchangecode(request["code"]);             }          }     } } 

if put breakpoint @ var result = ... line, can see request variables in immediate window, e.g.:

request["code"] "4/xugz-_zwfagpq4kbfas66pv0owj8........." request["email"] "gguuss@gmail.com" request["name"] "gus class (gus)" 

hope helps!

note: although need client id , client secret exchange one-time-code demonstrated in code, it's best practice protect best can. i'm using post in effort protect code in form should use https whenever passing around credentials.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -