public virtual void AssociateSocialAccountWithUser(User user, OpenAuthenticationParameters parameters)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            string email = null;

            if (parameters.UserClaims != null)
            {
                foreach (var userClaim in parameters.UserClaims.Where(x => x.Contact != null && !String.IsNullOrEmpty(x.Contact.Email)))
                {
                    email = userClaim.Contact.Email;
                    break;
                }
            }

            var socialRecord = new SocialRecord
            {
                UserId             = user.Id,
                Email              = email,
                Identifier         = parameters.Identifier,
                DisplayIdentifier  = parameters.DisplayIdentifier,
                OAuthToken         = parameters.OAuthToken,
                OAuthAccessToken   = parameters.OAuthAccessToken,
                ProviderSystemName = parameters.ProviderSystemName,
                CreatedOn          = DateTime.Now,
                ModifiedOn         = DateTime.Now
            };

            _socialRecordRepository.Insert(socialRecord);
        }
        public virtual void RemoveAssociation(OpenAuthenticationParameters parameters)
        {
            var record = _socialRecordRepository.Table
                         .FirstOrDefault(o => o.Identifier == parameters.Identifier &&
                                         o.ProviderSystemName == parameters.ProviderSystemName);

            if (record != null)
            {
                _socialRecordRepository.Delete(record);
            }
        }
        public virtual User GetUser(OpenAuthenticationParameters parameters)
        {
            var record = _socialRecordRepository.Table
                         .FirstOrDefault(o => o.Identifier == parameters.Identifier &&
                                         o.ProviderSystemName == parameters.ProviderSystemName);

            if (record != null)
            {
                return(_userService.GetUserById(record.UserId));
            }

            return(null);
        }
 public virtual bool AccountExists(OpenAuthenticationParameters parameters)
 {
     return(GetUser(parameters) != null);
 }
Esempio n. 5
0
        public virtual AuthorizationResult Authorize(OpenAuthenticationParameters parameters)
        {
            var userFound   = _openAuthenticationService.GetUser(parameters);
            var currentUser = _userContext.CurrentUser;

            if (AccountAlreadyExists(userFound, currentUser))
            {
                if (AccountIsAssignedToLoggedOnAccount(userFound, currentUser))
                {
                    return(new AuthorizationResult(OpenAuthenticationStatus.Authenticated));
                }

                var result = new AuthorizationResult(OpenAuthenticationStatus.Error);
                result.AddError("Account is already assigned");
                return(result);
            }
            if (AccountDoesNotExistAndUserIsNotLoggedOn(userFound, currentUser))
            {
                SocialAuthorizerHelper.StoreParametersForRoundTrip(parameters);

                #region Register user

                currentUser = _userContext.CurrentUser;
                var details        = new RegistrationDetails(parameters);
                var randomPassword = CommonHelper.GenerateRandomDigitCode(20);

                bool isApproved = true;

                var registrationRequest = new UserRegistrationRequest(currentUser,
                                                                      details.EmailAddress,
                                                                      details.UserName,
                                                                      randomPassword,
                                                                      isApproved);

                var registrationResult = _userService.RegisterUser(registrationRequest);
                if (registrationResult.Success)
                {
                    currentUser = registrationResult.User;
                    _openAuthenticationService.AssociateSocialAccountWithUser(currentUser, parameters);
                    SocialAuthorizerHelper.RemoveParameters();

                    //authenticate
                    if (isApproved)
                    {
                        _authenticationService.SignIn(userFound ?? currentUser, false);
                    }

                    if (isApproved)
                    {
                        //send user welcome message
                        _emailService.SendUserWelcomeMessage(currentUser);

                        //result
                        return(new AuthorizationResult(OpenAuthenticationStatus.AutoRegisteredStandard));
                    }
                }
                else
                {
                    SocialAuthorizerHelper.RemoveParameters();

                    var result = new AuthorizationResult(OpenAuthenticationStatus.Error);
                    foreach (var error in registrationResult.Errors)
                    {
                        result.AddError(string.Format(error));
                    }
                    return(result);
                }

                #endregion
            }
            if (userFound == null)
            {
                _openAuthenticationService.AssociateSocialAccountWithUser(currentUser, parameters);
            }

            //authenticate
            _authenticationService.SignIn(userFound ?? currentUser, false);
            return(new AuthorizationResult(OpenAuthenticationStatus.Authenticated));
        }
        public static void StoreParametersForRoundTrip(OpenAuthenticationParameters parameters)
        {
            var session = GetSession();

            session["ef.socialauth.parameters"] = parameters;
        }