Esempio n. 1
0
        public async Task <IdentityResult> CreateNewUserWithExternalLogin(ExternalLoginInfo info, string requestedOrganization)
        {
            var externalIdentity = info.ExternalIdentity;
            var userSettings     =
                _organizationDbSet.Where(o => o.ShortName == requestedOrganization)
                .Select(u => new { u.CultureCode, u.TimeZone })
                .First();

            var user = new ApplicationUser
            {
                UserName              = externalIdentity.FindFirst(ClaimTypes.Email).Value,
                Email                 = externalIdentity.FindFirst(ClaimTypes.Email).Value,
                FirstName             = externalIdentity.FindFirst(ClaimTypes.GivenName).Value,
                LastName              = externalIdentity.FindFirst(ClaimTypes.Surname).Value,
                OrganizationId        = _organizationService.GetOrganizationByName(requestedOrganization).Id,
                EmploymentDate        = DateTime.UtcNow,
                CultureCode           = userSettings.CultureCode ?? ConstBusinessLayer.DefaultCulture,
                TimeZone              = userSettings.TimeZone,
                NotificationsSettings = null
            };

            if (externalIdentity.FindFirst("picture") != null)
            {
                byte[] data    = data = await new WebClient().DownloadDataTaskAsync(externalIdentity.FindFirst("picture").Value);
                var    picture = await _pictureService.UploadFromStream(new MemoryStream(data), "image/jpeg", $"{Guid.NewGuid()}.jpg", user.OrganizationId);

                user.PictureId = picture;
            }

            var result = _userManager.Create(user);

            if (!result.Succeeded)
            {
                return(result);
            }

            AddNewUserRoles(user.Id);
            return(result);
        }