public async Task <ActionResult> GoogleCallback() { var GoogleEndpointsHelper = new GoogleEndpointsHelper(); var state = Request.QueryString["state"]; var code = Request.QueryString["code"]; var values = new Dictionary <string, string> { { "code", code }, { "redirect_uri", AuthorizeResponseUrl }, { "client_id", GoogleApiKey }, { "client_secret", GoogleClientSecret }, { "grant_type", "authorization_code" }, { "scope", "" } }; var webmethods = new WebMethodsHelper(); var tokenResponse = await webmethods.Post(GoogleEndpointsHelper.TokenEndpoint, values); var jobject = JObject.Parse(tokenResponse); var access_token = jobject.SelectToken("access_token"); var refresh_token = jobject.SelectToken("refresh_token"); if (access_token == null || access_token.ToString().Trim().Length == 0) { TempData["message"] = "Sorry, something went wrong, try a different method for logging in."; //Response.Redirect(SitePathsHelper.CustomerLoginPath); } string id = ""; string email = ""; string firstName = ""; string surname = ""; using (var client = new WebClient()) { var response = client.DownloadString($"https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token={access_token}"); var profileResponseJObject = JObject.Parse(response); id = profileResponseJObject.SelectToken("id").ToString(); email = profileResponseJObject.SelectToken("email").ToString(); firstName = profileResponseJObject.SelectToken("given_name").ToString(); surname = profileResponseJObject.SelectToken("family_name").ToString(); } if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(email)) { //something went wrong, take user back to login page and display error TempData["message"] = "Sorry, something went wrong. Please try a different method for logging in."; //Response.Redirect(SitePathsHelper.CustomerLoginPath); } //search for registered user by their facebook id return(View()); }
public ActionResult GoogleLogin() { var GoogleEndpointsHelper = new GoogleEndpointsHelper(); var url = GoogleEndpointsHelper.AuthorizationEndpoint + "?" + "client_id=" + GoogleApiKey + "&" + "response_type=code&" + "scope=openid%20email%20profile&" + "redirect_uri=" + AuthorizeResponseUrl + "&" + //"state=" + Session["SessionId"] + "&" + //"login_hint=" + Session["Email"] + "&" + "access_type=offline"; return(Redirect(url)); }