Esempio n. 1
0
        public async Task <IActionResult> Onlogon(string code, string scope)
        {
            HttpClient client =
                new HttpClient();

            HttpResponseMessage res = await
                                      client.PostAsync("https://oauth2.googleapis.com/token?" +
                                                       "client_id=834829378246-po41ui5mdf7hn1dqt58785f81r25rqh7.apps.googleusercontent.com" +
                                                       $"&client_secret=" + await _googleSecretKeyProvider.GetKey() +
                                                       $"&code={code}" +
                                                       $"&grant_type=authorization_code" +
                                                       $"&redirect_uri=http://localhost:22111/GoogleAccount/OnLogon",
                                                       null);

            if (res.IsSuccessStatusCode)
            {
                var googleResultStr =
                    await res.Content.ReadAsStringAsync();

                GoogleSuccessResultModel model =
                    JsonConvert.DeserializeObject <GoogleSuccessResultModel>(googleResultStr);

                //HttpContent c =
                //	new StringContent(googleResultStr, Encoding.UTF8, "application/json");

                //var xxx = await
                //	client.PostAsync("http://localhost:22111/GoogleAccount/TokenForGoogleUser", c);

                var xxx =
                    await GetTokenForGoogleUser(model);

                //var userWithTokenStr =
                //	await xxx.Content.ReadAsStringAsync();

                //var userWithToken =
                //	JsonConvert.DeserializeObject<UserModel>(userWithTokenStr);
            }

            return(Ok());
        }
Esempio n. 2
0
        private async Task <string> GetTokenForGoogleUser(GoogleSuccessResultModel model)
        {
            HttpClient client =
                new HttpClient();

            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {model.access_token}");

            var metaResult =
                await client.GetAsync("https://people.googleapis.com/v1/people/me" +
                                      "?personFields=names");

            if (metaResult.IsSuccessStatusCode)
            {
                var metaContent =
                    await metaResult.Content.ReadAsStringAsync();

                var metaInfo =
                    JsonConvert.DeserializeObject <GoogleMetaInfo>(metaContent);

                var meta =
                    metaInfo.names.Single();

                var googleId =
                    meta.metadata.source.id;

                var userName =
                    meta.displayName;

                var token =
                    await _jwtGenerator.Generate(userName, "user", "");

                return(token);
            }


            throw new InvalidOperationException();
        }