예제 #1
0
        ///<inheritdoc/>
        public async Task <UserDTO> GetGoogleUserAsync(string providerToken)
        {
            string googleApiTokenInfoUrl =
                ConfigSettingLayoutRenderer.DefaultConfiguration.GetSection("GoogleAuthentication")["GoogleApiTokenInfoUrl"];
            var httpClient = new HttpClient();
            var requestUri = new Uri(string.Format(googleApiTokenInfoUrl, providerToken));

            HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(requestUri);

            if (httpResponseMessage.StatusCode != HttpStatusCode.OK)
            {
                throw new HttpRequestException("Status code isn`t correct");
            }

            var response = await httpResponseMessage.Content.ReadAsStringAsync();

            var googleApiTokenInfo = JsonConvert.DeserializeObject <GoogleApiTokenInfo>(response);
            var user = await _userManager.FindByEmailAsync(googleApiTokenInfo.Email);

            if (user == null)
            {
                user = new User
                {
                    UserName         = googleApiTokenInfo.Email,
                    Email            = googleApiTokenInfo.Email,
                    FirstName        = googleApiTokenInfo.GivenName,
                    LastName         = googleApiTokenInfo.FamilyName ?? googleApiTokenInfo.GivenName,
                    SocialNetworking = true,
                    ImagePath        = "default_user_image.png",
                    EmailConfirmed   = true,
                    RegistredOn      = DateTime.Now,
                    UserProfile      = new UserProfile()
                };
                var createResult = await _userManager.CreateAsync(user);

                if (createResult.Succeeded)
                {
                    var emailContent = _emailContentService.GetAuthGoogleRegisterEmail();
                    await _emailSendingService.SendEmailAsync(user.Email, emailContent.Subject, emailContent.Message, emailContent.Title);
                }
                else
                {
                    throw new ArgumentException("Failed creation of user");
                }
                await _userManager.AddToRoleAsync(user, Roles.RegisteredUser);
            }
            await _signInManager.SignInAsync(user, isPersistent : false);

            return(_mapper.Map <User, UserDTO>(user));
        }