Example #1
0
        private async Task <User> GetGithubUserInfo(Core.CSharp.Models.ExternalLoginInfo externalLoginInfo)
        {
            ExternalEmailSecret githubSecrets = AppConst.Settings.ExternalLoginSecrets.FindObj(e => e.Provider.EqualCurrentCultureIgnoreCase("Github"));

            var caller = new HttpClient();
            // get the token from github API by using the provided "code", "state", "clientId" and "clientSecret".
            var tokenResult = await caller.GetAsync($"https://github.com/login/oauth/access_token?client_id={githubSecrets?.ClientId}&client_secret={githubSecrets?.ClientSecret}&code={externalLoginInfo.Code}&state={externalLoginInfo.State}").ConfigureAwait(false);

            var tokenResultArray = (await tokenResult.Content.ReadAsStringAsync().ConfigureAwait(false))
                                   .Replace("access_token", "").Replace("&scope", "").Replace("token_type", "").Split("=");

            // Add the authorization information from the response above
            caller.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tokenResultArray[3], tokenResultArray[1]);
            caller.DefaultRequestHeaders.Add("User-Agent", "TwoMJ.Web.App (test app)");

            // get the user info by calling the github API with the above authorization
            var userInfoResult = await caller.GetAsync("https://api.github.com/user").ConfigureAwait(false);

            var userInfoResultString = await userInfoResult.Content.ReadAsStringAsync().ConfigureAwait(false);

            var userInfoObj = JsonConvert.DeserializeObject <dynamic>(userInfoResultString);

            return(new User
            {
                Email = (string)userInfoObj.email,
                RegistrationMethod = new RegistrationMethod
                {
                    ExternalLinkedId = (string)userInfoObj.id,
                    RegisteredDate = DateTime.UtcNow,
                    Type = externalLoginInfo.Type,
                }
            });
        }
Example #2
0
        private async Task <User> GetGoogleUserInfo(Core.CSharp.Models.ExternalLoginInfo externalLoginInfo)
        {
            ExternalEmailSecret googleSecrets = AppConst.Settings.ExternalLoginSecrets.FindObj(e => e.Provider.EqualCurrentCultureIgnoreCase("Google"));

            var caller  = new HttpClient();
            var content = new StringContent($"client_id={googleSecrets?.ClientId}&client_secret={googleSecrets?.ClientSecret}&code={externalLoginInfo.Code}&grant_type=authorization_code&redirect_uri={googleSecrets.RedirectURI}");

            content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
            // get the token from github API by using the provided "code", "state", "clientId" and "clientSecret".
            var tokenResult = await caller.PostAsync("https://oauth2.googleapis.com/token", content).ConfigureAwait(false);

            var    tokenResultArray = JsonConvert.DeserializeObject <dynamic>(await tokenResult.Content.ReadAsStringAsync().ConfigureAwait(false));
            string tokenType        = tokenResultArray.token_type;
            string accessToken      = tokenResultArray.access_token;

            // Add the authorization information from the response above
            caller.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tokenType, accessToken);
            caller.DefaultRequestHeaders.Add("User-Agent", "TwoMJ.Web.App (test app)");
            // get the user info by calling the github API with the above authorization
            var userInfoResult = await caller.GetAsync($"https://www.googleapis.com/oauth2/v2/userinfo").ConfigureAwait(false);

            var userInfoResultString = await userInfoResult.Content.ReadAsStringAsync().ConfigureAwait(false);

            var userInfoObj = JsonConvert.DeserializeObject <dynamic>(userInfoResultString);

            return(new User
            {
                Email = (string)userInfoObj.email,
                FirstName = (string)userInfoObj.given_name,
                Surname = (string)userInfoObj.family_name,
                RegistrationMethod = new RegistrationMethod
                {
                    ExternalLinkedId = (string)userInfoObj.id,
                    RegisteredDate = DateTime.UtcNow,
                    Type = externalLoginInfo.Type,
                }
            });
        }
        private static async Task <User> GetFacebookUserInfo(ExternalLoginDetails externalLoginInfo)
        {
            ExternalEmailSecret facebookSecrets = AppConst.Settings.ExternalLoginSecrets.FindObj(e => e.Provider.EqualCurrentCultureIgnoreCase("Facebook"));

            var caller = new HttpClient();
            // get the token from github API by using the provided "code", "state", "clientId" and "clientSecret".
            var tokenResult = await caller.GetAsync($"https://graph.facebook.com/v7.0/oauth/access_token?client_id={facebookSecrets?.ClientId}&redirect_uri={externalLoginInfo.RedirectUrl}&client_secret={facebookSecrets?.ClientSecret}&code={externalLoginInfo.Code}").ConfigureAwait(false);

            var    tokenResultArray = JsonConvert.DeserializeObject <dynamic>(await tokenResult.Content.ReadAsStringAsync().ConfigureAwait(false));
            string tokenType        = tokenResultArray.token_type;
            string accessToken      = tokenResultArray.access_token;

            // Add the authorization information from the response above
            caller.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tokenType, accessToken);
            caller.DefaultRequestHeaders.Add("User-Agent", "OSnack");

            // get the user info by calling the github API with the above authorization
            var userInfoResult = await caller.GetAsync($"https://graph.facebook.com/me?fields=id,email,first_name,last_name&access_token={accessToken}").ConfigureAwait(false);

            var userInfoResultString = await userInfoResult.Content.ReadAsStringAsync().ConfigureAwait(false);

            var userInfoObj = JsonConvert.DeserializeObject <dynamic>(userInfoResultString);

            return(new User
            {
                FirstName = (string)userInfoObj.first_name,
                Surname = (string)userInfoObj.last_name,
                Email = (string)userInfoObj.email,
                RegistrationMethod = new RegistrationMethod
                {
                    ExternalLinkedId = (string)userInfoObj.id,
                    RegisteredDate = DateTime.UtcNow,
                    Type = externalLoginInfo.Type,
                }
            });
        }