Exemplo n.º 1
0
        public async Task<string> SetUserAsync(DomainUser user, TokenData tokenData, bool isPersistent = false)
        {
            if (tokenData != null && tokenData.IdentityProvider != ProviderType.Email)
            {
                await _userRepository.UpdateMembershipAsync(user.Id, _mapper.Map<TokenData, UserMembershipEntity>(tokenData));
            }

            string identityProvider = tokenData == null ? null : tokenData.IdentityProvider.ToString();
            return _authenticator.Authenticate(_mapper.Map<DomainUser, UserEntity>(user), identityProvider, isPersistent);
        }
Exemplo n.º 2
0
        public async Task SendWelcomeMessageAsync(DomainUser user, TokenData data)
        {
            string facebookMessage = _settings.FacebookRegistrationMessage;
            if (String.IsNullOrEmpty(facebookMessage))
            {
                return;
            }

            if (data == null || string.IsNullOrEmpty(data.Token))
            {
                throw new ArgumentNullException("data");
            }

            JObject message = JObject.Parse(facebookMessage);

            var fb = new FacebookClient(data.Token);
            var post = new
            {
                caption = (string)message["caption"],
                message = (string)message["message"],
                name = (string)message["name"],
                description = (string)message["description"],
                picture = (string)message["picture"],
                link = (string)message["link"]
            };

            try
            {
                await fb.PostTaskAsync("me/feed", post);
            }
            catch (FacebookOAuthException e)
            {
                //Permission error
                if (e.ErrorCode != 200)
                {
                    throw new BadRequestException(e);
                }
            }
            catch (WebExceptionWrapper e)
            {
                throw new BadGatewayException(e);
            }
        }
        public TokenData Get(IpData data)
        {
            var client = new FacebookClient(data.Token);
            var result = new TokenData();

            dynamic userData;

            try
            {
                userData = client.Get("me");
            }
            catch (FacebookOAuthException e)
            {
                throw new BadRequestException(e);
            }
            catch (WebExceptionWrapper e)
            {
                throw new BadGatewayException(e);
            }

            result.IdentityProvider = ProviderType.Facebook;
            result.Name = userData.name;
            result.UserIdentifier = userData.id;
            result.Token = data.Token;
            result.TokenSecret = data.TokenSecret;

            // If email was not requested
            try
            {
                result.Email = userData.email;
            }
            catch
            {
                result.Email = null;
            }

            return result;
        }
Exemplo n.º 4
0
 private DomainUser CreateDomainUser(TokenData tokenData)
 {
     return new DomainUser
     {
         ApplicationName = AppName,
         Name = tokenData.Name,
         Email = tokenData.Email,
         Memberships = new List<UserMembership>
         {
             new UserMembership
             {
                 IdentityProvider = tokenData.IdentityProvider,
                 UserIdentifier = tokenData.UserIdentifier
             }
         },
         Roles = new List<string> { DomainRoles.User }
     };
 }
Exemplo n.º 5
0
        private async Task<DomainUser> CompleteRegistrationAsync(TokenData tokenData)
        {
            DomainUser user = null;

            // Create new profile
            try
            {
                user = await _userService.AddAsync(CreateDomainUser(tokenData));
            }
            catch (Exception e)
            {
                Trace.TraceError("Authentication failed for '{0}:{1}': {2}", tokenData.IdentityProvider, tokenData.UserIdentifier, e);
                throw;
            }

            // Send registration e-mail
            try
            {
                await _emailNotificationService.SendRegistrationEmailAsync(user);
            }
            catch (Exception e)
            {
                Trace.TraceError("Failed to send registration email to address {0} for user {1}: {2}", user.Email, user.Id, e);
            }

            // Send notification into social network
            ISocialNetworkNotifier notifier = _notificationFactory.GetNotifier(tokenData.IdentityProvider);
            if (notifier != null)
            {
                try
                {
                    await notifier.SendWelcomeMessageAsync(user, tokenData);
                }
                catch (BadGatewayException)
                {
                }
                catch (Exception e)
                {
                    Trace.TraceError("Failed to send welcome message to user '{0}': {1}", user.Id, e);
                }
            }

            //For statistics
            HttpContext.Items.Add("isRegister", true);

            return user;
        }
Exemplo n.º 6
0
        private async Task<DomainUser> GetUserByEmail(TokenData tokenData)
        {
            DomainUser user = null;
            if (string.IsNullOrEmpty(tokenData.Email))
            {
                return null;
            }

            try
            {
                user = await _userService.FindByEmailAsync(tokenData.Email);
            }
            catch (NotFoundException)
            {
            }
            catch (Exception e)
            {
                Trace.TraceError("Failed to find user by email '{0}': {1}", tokenData.Email, e);
                throw;
            }

            return user;
        }
Exemplo n.º 7
0
        private async Task<DomainUser> GetUserByToken(TokenData tokenData)
        {
            DomainUser user = null;
            try
            {
                user = await _userService.FindByIdentityAsync(tokenData.IdentityProvider, tokenData.UserIdentifier);
            }
            catch (NotFoundException)
            {
            }
            catch (Exception e)
            {
                Trace.TraceError("Failed to validate ip user '{0}:{1}': {2}", tokenData.IdentityProvider, tokenData.UserIdentifier, e);
                throw;
            }

            return user;
        }
Exemplo n.º 8
0
 public Task AddMembersipAsync(string userId, TokenData tokenData)
 {
     return _userRepository.AddMembershipAsync(userId, _mapper.Map<TokenData, UserMembershipEntity>(tokenData));
 }