Exemple #1
0
        public HttpResponseMessage GoogleSignin(GoogleSigninRequest model)
        {
            bool authToken = usersService.GoogleSignin(model);

            if (!authToken)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User cannot be authenticated"));
            }
            return(Request.CreateResponse(HttpStatusCode.OK, authToken));
        }
Exemple #2
0
        public bool GoogleSignin(GoogleSigninRequest model)
        {
            bool userAuthenticated = false;
            int  userId            = 0;

            string         googleClientId = "58772775873-oma31jtiqhph7os62h7i9a37makcilfr.apps.googleusercontent.com";
            string         gapiRespObject;
            string         gapiAuthUrl = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=";
            HttpWebRequest webReq      = (HttpWebRequest)WebRequest.Create(gapiAuthUrl + model.GoogleToken);

            webReq.Method = "GET";
            HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();

            using (Stream stream = webResp.GetResponseStream())
            {
                StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
                gapiRespObject = reader.ReadToEnd();
            }

            var    gapiRespString = (JObject)JsonConvert.DeserializeObject(gapiRespObject);
            string authAud        = gapiRespString["aud"].Value <string>();
            string authFirstName  = gapiRespString["given_name"].Value <string>();
            string authLastName   = gapiRespString["family_name"].Value <string>();
            string authImage      = gapiRespString["picture"].Value <string>();
            string authEmail      = gapiRespString["email"].Value <string>();

            if (authAud == googleClientId)
            {
                userAuthenticated = true;

                dataProvider.ExecuteProcedure(
                    "Users_GoogleSignin",
                    inputParamMapper: (parameters) =>
                {
                    parameters.AddWithValue("@FirstName", authFirstName);
                    parameters.AddWithValue("@LastName", authLastName);
                    parameters.AddWithValue("@ImageURL", authImage);
                    parameters.AddWithValue("@Email", authEmail);
                    parameters.AddWithValue("@GoogleId", authAud);
                },
                    rowMapper: (parameters) =>
                {
                    userId = (int)parameters["Id"];
                });

                FormsAuthentication.SetAuthCookie(Convert.ToString(userId), true);
            }
            return(userAuthenticated);
        }