private async Task <NormalizedSocialUserData> GetUserVkInfoAsync(long externalUserId, string email, string externalToken, long expiresIn) { _logger.LogInformation($"{nameof(VkService)}.{nameof(GetUserVkInfoAsync)}.Start"); var userInfoResponse = await Client.GetStringAsync( $"https://api.vk.com/method/users.get?user_ids={externalUserId}&lang=0&fields=first_name,last_name,sex,bdate,city,photo_max_orig&access_token={externalToken}&v=5.80"); _logger.LogInformation($"{nameof(VkService)}.{nameof(GetUserInfoAsync)}. UserInfoResponse = {userInfoResponse}"); var userInfo = JsonConvert.DeserializeObject <VkUserDataResponse>(userInfoResponse).Response.FirstOrDefault(); var result = new NormalizedSocialUserData { ExternalId = userInfo.UserId, ExternalEmail = email, ExternalToken = externalToken, ExpiresIn = expiresIn, FirstName = userInfo.FirstName, LastName = userInfo.LastName, BirthDate = userInfo.Birthday, SexType = userInfo.Sex, City = userInfo.City?.Title, OriginalPhotoUrl = userInfo.OriginalPhoto }; _logger.LogInformation($"{nameof(VkService)}.{nameof(GetUserVkInfoAsync)}.End"); return(result); }
private async Task <UserDto> ExternalUserProcessing(NormalizedSocialUserData socialUserData, SocialType socialType) { var user = await Db.Socials.FindUserByExternalUserIdAsync(socialUserData.ExternalId, socialType); if (user != null) { if (socialUserData.ExpiresIn == 0) { await Db.Socials.UpdateExternalAccessToken(socialUserData.ExternalId, socialType, socialUserData.ExternalToken, socialUserData.ExpiresIn); } return(user); } user = await FindUserByEmail(socialUserData.ExternalEmail); if (user == null) { return(await RegisterViaExternalSocial(socialUserData, socialType)); } if (!await AddExternalEnter(user.UserId, socialType, socialUserData)) { throw new Exception( $"We can not your information about your {socialType} account in our system. Please, log in via email or phone or change social network to another."); } return(user); }
private async Task <NormalizedSocialUserData> GetUserFbInfoAsync(string externalToken, int expiresIn) { var userInfoResponse = await Client.GetStringAsync( $"https://graph.facebook.com/v3.0/me?fields=id,email,first_name,last_name,name,gender,locale,hometown,birthday,picture&access_token={externalToken}"); var userInfo = JsonConvert.DeserializeObject <FacebookUserResponse>(userInfoResponse); //var smallUrlResponse = // await Client.GetStringAsync($"https://graph.facebook.com/v3.0/{userInfo.ExternalId}/picture?type=normal"); //var smallUrl = JsonConvert.DeserializeObject<FacebookPictureResponse>(smallUrlResponse).Response.Url; //var largeUrlResponse = // await Client.GetStringAsync($"https://graph.facebook.com/v3.0/{userInfo.ExternalId}/picture?type=large"); //var largeUrl = JsonConvert.DeserializeObject<FacebookPictureResponse>(largeUrlResponse).Response.Url; var result = new NormalizedSocialUserData { ExternalId = userInfo.ExternalId, ExternalEmail = userInfo.Email, ExternalToken = externalToken, ExpiresIn = expiresIn, FirstName = userInfo.FirstName, LastName = userInfo.LastName, BirthDate = userInfo.Birthday, SexType = userInfo.SexType, City = userInfo.City?.Title, //SmallPhotoUrl = $"https://graph.facebook.com/v3.0/{userInfo.ExternalId}/picture?type=normal", OriginalPhotoUrl = $"https://graph.facebook.com/v3.0/{userInfo.ExternalId}/picture?type=large" }; return(result); }
private async Task <UserDto> RegisterViaExternalSocial(NormalizedSocialUserData user, SocialType socialType) { const RoleType role = RoleType.User; var userDb = new UserEntity { Email = user.ExternalEmail, RoleType = role, RegistrationDate = DateTime.UtcNow }; var isAdded = await Db.Users.AddUserAsync(userDb); if (!isAdded || userDb.UserId == 0) { throw new Exception("We are not able to add you. Please, tell us about it."); } var fullUrl = await UploadAvatarUrlOrGetStandard(user.OriginalPhotoUrl); var userInfo = new UserInfoQuery { UserId = userDb.UserId, FirstName = user.FirstName, LastName = user.LastName, BirthDate = user.BirthDate, SexType = user.SexType, City = user.City, OriginalAvatarUrl = fullUrl, Description = "" }; if (!await Db.UsersInfo.AddUserInfoAsync(userInfo)) { await Db.Users.RemoveUserAsync(userDb.UserId); throw new Exception("We are not able to add your information. Please, tell us about it."); } if (!await AddUserSocialAsync(userDb.UserId, socialType, user)) { await Db.Users.RemoveUserAsync(userDb.UserId); throw new Exception("We are not able to add your social information. Please, tell us about it."); } await Hub.Monitoring.AddUrlMonitoring(userDb.UserId); return(new UserDto { UserId = userDb.UserId, RoleType = userDb.RoleType }); }
public async Task <bool> AddUserSocialAsync(int internalId, SocialType socialType, NormalizedSocialUserData socialUserData) { Logger.LogInformation($"{nameof(ExternalAuthService)}.{nameof(AddUserSocialAsync)}.Start"); var social = new SocialEntity { InternalId = internalId, SocialType = socialType, ExternalId = socialUserData.ExternalId, Email = socialUserData.ExternalEmail, ExternalToken = socialUserData.ExternalToken, ExpiresIn = socialUserData.ExpiresIn }; var result = await Db.Socials.AddUserSocialAsync(social); Logger.LogInformation($"{nameof(ExternalAuthService)}.{nameof(AddUserSocialAsync)}.End"); return(result); }
private async Task <bool> AddExternalEnter(int userId, SocialType socialType, NormalizedSocialUserData socialUserData) { if (!await AddUserSocialAsync(userId, socialType, socialUserData)) { return(false); } var userInfo = await Db.UsersInfo.GetUserInfoAsync(userId); if (userInfo.OriginalAvatarUrl.IsNullOrEmpty() || MediaConverter.IsStandardBackground(userInfo.OriginalAvatarUrl) && !socialUserData.OriginalPhotoUrl.IsNullOrEmpty()) { userInfo.OriginalAvatarUrl = await UploadAvatarUrlOrGetStandard(socialUserData.OriginalPhotoUrl); } await Db.UsersInfo.UpdateUserInfoAsync(userInfo); return(true); }