-1

enter image description hereI am trying to implement Google sign in into my web forms page because we use G suite for business. basically I am using javascript to get the token then using ajax to post to the google api then I am looking at the HD claim to make sure they are apart of our domain and thne posting to the code behind the email and doing a lookup to get a user ID then setting the form cookie and trying to redirect to our defalut page. So we have a mvc app that I done this exact thing with and it works perfectly I just routed them to the controller. For some reason it just seems like I am not getting anything from my code behind code.

here is my javascript.

  <script>
        function onSignIn(googleUser) {
            debugger;
            const profile = googleUser.getBasicProfile();
            let boolRedirectUrl = false;
            const id_token = googleUser.getAuthResponse().id_token;
            const pathname = window.location.pathname;
            const url = window.location.href;
            let redirectPath = "";
            if (window.location.href.indexOf("ReturnUrl") > -1) {
                boolRedirectUrl = true;
                redirectPath = readQueryString("ReturnUrl");
            }
            const googleUrl = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=" + id_token;
            $.ajax({
                url: googleUrl,
                dataType: 'json',
                data: '{}',
                contentType: 'application/json',
                success: (data, status, xhr) => {
                    const domain = data.hd;
                    const email = data.email;
                    if (domain === "kimbelmechanical.com") {
                        $.ajax({
                            url: "Login.aspx/GoogleLogin",
                            type: "POST",
                            data: { 'email': email },
                            success: (data, status, xhr) => {
                                //window.location.href = "Default.aspx"
                            },
                            error: (xhr, status, error) => {
                                console.log("I stopeed on the call to controller " + status);
                            }
                        });

                    }
                },
                error: (xhr, status, error) => {
                    console.log(status);
                }
            });
        };

        function readQueryString(key) {
            key = key.replace(/[*+?^$.\[\]{}()|\\\/]/g, "\\$&");
            var match = location.search.match(new RegExp("[?&]" + key + "=([^&]+)(&|$)"));
            return match && decodeURIComponent(match[1].replace(/\+/g, " "));
        }
    </script>

this is my c# code behind.

 [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void GoogleLogin(string email)
    {
        string userID = "";
        using (var context = new KPDataContext(KP.Common.KPConnectionString))
        {
            var user = context.Employees.Where(p => p.Email == email);

            foreach (var e in user)
            {
                userID = e.Username;
            }
        }




        if (userID != "")
        {
            FormsAuthentication.SetAuthCookie(userID, false);
            Response.Redirect("~/Default.aspx", true);
        }
    }

if I redirect from the success to my default page it loads it default.aspx found but for some reason it does login.aspx?ReturnUrl=?default.aspx but stays on the login page so I am in a endless loop of loading the login page over and over.

user3263515
  • 57
  • 1
  • 9
  • 4
    You cannot redirect from an AJAX request... Use the success event handler to redirect from JavaScript – Camilo Terevinto Jul 05 '18 at 12:37
  • Possible duplicate of https://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call – DavidG Jul 05 '18 at 12:39
  • so I tried that but for some reason I was in this endless loop of it going back to my login page. and for some reason when I put break points to hit my code behind it never hits my breakpoint even though my network tab does get a 200 ok response. – user3263515 Jul 05 '18 at 12:40
  • Possible duplicate of [How to manage a redirect request after a jQuery Ajax call](https://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call) – Camilo Terevinto Jul 05 '18 at 12:40
  • Why do you want to redirect at all though? The whole purpose of AJAX is to make it possible to avoid postbacks, refreshes, redirects of any kind and **stay on the same page**. If you want/ need to redirect, forget the ajax and just use a normal form submit to post back and then redirect from there. – ADyson Jul 05 '18 at 12:59
  • I want to redirect because I want them to go to my main page after they are signed in using google. my cookie that I set is an authorization saying they are authorized and apart of our organization. – user3263515 Jul 05 '18 at 13:22
  • you don't need AJAX to implement a google sign in – ADyson Jul 05 '18 at 14:15
  • I know you don't but this is the simplest way to get the cliam and to make sure someone is part of my domain and this works really well in mvc app but not sure why it doesn't work here – user3263515 Jul 05 '18 at 15:08

1 Answers1

0

Is it MVC? If so you can use RouteData along with creating a new instance of a controller. That controller can then execute a new web request with the new path like so:

 var routeData = new RouteData();
                    routeData.Values.Add("message", errorModel.message);
                    routeData.Values.Add("action", "Index");
                    routeData.Values.Add("controller", "ErrorPage");
                    IController ctrl = new ErrorController();
                    ctrl.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
  Response.End();