public async Task <IHttpActionResult> RegisterExternal()
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var info = await Authentication.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(InternalServerError());
            }

            if (!await _administrationService.UserEmailExistsAsync(info.Email))
            {
                if (await _administrationService.UserIsSoftDeletedAsync(info.Email))
                {
                    await _administrationService.RestoreUserAsync(info.Email);
                }
                else
                {
                    var requestedOrganization = RequestedOrganization;
                    var result = await _administrationService.CreateNewUserWithExternalLoginAsync(info, requestedOrganization);

                    if (!result.Succeeded)
                    {
                        return(GetErrorResult(result));
                    }
                }
            }
            else if (await _administrationService.HasExistingExternalLoginAsync(info.Email, info.Login.LoginProvider))
            {
                var user = await _userManager.FindByEmailAsync(info.Email);

                await _administrationService.AddProviderImageAsync(user.Id, info.ExternalIdentity);

                return(Ok("User already exists"));
            }
            else if (await _administrationService.HasExistingExternalLoginAsync(info.Email, AuthenticationConstants.InternalLoginProvider))
            {
                var user = await _userManager.FindByEmailAsync(info.Email);

                if (user?.EmailConfirmed == false)
                {
                    await _userManager.RemoveLoginAsync(user.Id, new UserLoginInfo(AuthenticationConstants.InternalLoginProvider, user.Id));

                    await _userManager.RemovePasswordAsync(user.Id);
                }
            }

            var userId = (await _userManager.FindByEmailAsync(info.Email)).Id;
            await _userManager.AddLoginAsync(userId, info.Login);

            await _administrationService.AddProviderImageAsync(userId, info.ExternalIdentity);

            await _administrationService.AddProviderEmailAsync(userId, info.Login.LoginProvider, info.Email);

            return(Ok());
        }
Beispiel #2
0
        public async Task RemoveLoginAsync(string id, UserLoginInfo loginInfo)
        {
            await _userManager.RemoveLoginAsync(id, loginInfo);

            var user = await _usersDbSet.FirstAsync(u => u.Id == id);

            if (loginInfo.LoginProvider == "Google")
            {
                user.GoogleEmail = null;
            }

            if (loginInfo.LoginProvider == "Facebook")
            {
                user.FacebookEmail = null;
            }

            await _uow.SaveChangesAsync(id);
        }