private User GetExistingUserFromProfile(OpenIdProfile profile)
 {
     var openIdentifier = _repository.All<OpenIdentifier>().ByIdentifier(profile.Identifier);
     return openIdentifier == null ? null : _repository.All<User>().ById(openIdentifier.UserId);
 }
        private User CreateUserFromProfile(OpenIdProfile profile)
        {
            if (_repository.All<OpenIdentifier>().ByIdentifier(profile.Identifier) != null)
                throw new InvalidOperationException(
                    "The openId '{0}' is already associated with another user.".ToFormat(profile.Identifier));

            var openIdentifier = new OpenIdentifier
                                     {
                                         Identifier = profile.Identifier,
                                         IsPrimary = true
                                     };

            var user = new User
                           {
                               BirthDate = profile.Birthday,
                               DateCreated = DateTime.Now,
                               DisplayName = (!string.IsNullOrEmpty(profile.DisplayName)
                                                ? profile.DisplayName : profile.PreferredUserName) + "",
                               Location =
                                   (profile.Address != null
                                       ? (profile.Address.Locality + " " + profile.Address.Region).Trim()
                                       : string.Empty) + "",
                               Email =
                                   (string.IsNullOrEmpty(profile.VerifiedEmail)
                                       ? profile.Email
                                       : profile.VerifiedEmail) + "",
                               LastVisit = DateTime.Now,
                               Photo = profile.Photo + "",
                               PreferredUserName = profile.PreferredUserName + "",
                               RealName = (profile.Name != null ? profile.Name.FormattedName : string.Empty) + "",
                               Reputation = 0
                           };

            if (user.DisplayName.Trim() == string.Empty)
                user.DisplayName = "Uknown";

            using (var tx = _repository.BeginTransaction())
            {
                _repository.Save(user);

                openIdentifier.UserId = user.Id;

                _repository.Save(openIdentifier);

                _repository.Save(new ActionItem{Action = ActionType.Registered, DateCreated = DateTime.Now, UserId = user.Id});

                tx.Commit();
            }

            Rpx.Map(profile.Identifier, user.Id.ToString(System.Globalization.CultureInfo.CurrentCulture));

            return user;
        }
        public User LinkAccounts(int userId, OpenIdProfile profile)
        {
            if (profile == null)
                throw new ArgumentNullException("profile");

            var user = _repository.All<User>().ById(userId);
            if (user == null)
                throw new ArgumentException("A user with the id '{0}' does not exist.");

            var openId = _repository.All<OpenIdentifier>().ByIdentifier(profile.Identifier);
            if (openId != null)
                throw new InvalidOperationException("The OpenId you are trying tolink already exists.");

            openId = new OpenIdentifier {Identifier = profile.Identifier, IsPrimary = false, UserId = user.Id};

            _repository.Save(openId);

            return user;
        }
        private static OpenIdProfile MapFields(XContainer xProfileData)
        {
            var profile = new OpenIdProfile();
            var el = xProfileData.Element("identifier");
            if (el != null) profile.Identifier = el.Value;

            el = xProfileData.Element("providerName");
            if (el != null) profile.ProviderName = el.Value;

            el = xProfileData.Element("displayName");
            if (el != null) profile.DisplayName = el.Value;

            el = xProfileData.Element("preferredUsername");
            if (el != null) profile.PreferredUserName = el.Value;

            el = xProfileData.Element("gender");
            if (el != null) profile.Gender = el.Value;

            el = xProfileData.Element("birthday");
            if (el != null)
            {
                DateTime birthday;
                if (DateTime.TryParse(el.Value, out birthday))
                {
                    profile.PrimaryKey = el.Value;
                }
            }

            el = xProfileData.Element("utcOffset");
            if (el != null) profile.UtcOffset = el.Value;

            el = xProfileData.Element("email");
            if (el != null) profile.Email = el.Value;

            el = xProfileData.Element("verifiedEmail");
            if (el != null) profile.VerifiedEmail = el.Value;

            el = xProfileData.Element("url");
            if (el != null) profile.Url = el.Value;

            el = xProfileData.Element("phoneNumber");
            if (el != null) profile.PhoneNumber = el.Value;

            el = xProfileData.Element("photo");
            if (el != null) profile.Photo = el.Value;

            el = xProfileData.Element("primaryKey");
            if (el != null) profile.PrimaryKey = el.Value;

            return profile;
        }
        public User GetUserFromProfile(OpenIdProfile profile)
        {
            if (profile == null)
                throw new ArgumentNullException("profile");

            using (_repository.OpenSession())
            {
                var user = GetExistingUserFromProfile(profile);
                if (user != null)
                {
                    if(string.IsNullOrEmpty(user.Email))
                    {
                        user.Email = (string.IsNullOrEmpty(profile.VerifiedEmail)
                                         ? user.Email = profile.Email
                                         : profile.VerifiedEmail) + "";
                    }
                    if(string.IsNullOrEmpty(user.RealName))
                    {
                        if (profile.Name != null)
                            user.RealName = profile.Name.FormattedName + "";
                        else
                            user.RealName = profile.DisplayName + "";
                    }
                    if(string.IsNullOrEmpty(user.DisplayName))
                    {
                        if (!string.IsNullOrEmpty(profile.DisplayName))
                            user.DisplayName = profile.DisplayName;
                        else if (profile.Name != null)
                            user.DisplayName = profile.Name.FormattedName;
                        else if (!string.IsNullOrEmpty(profile.PreferredUserName))
                            user.DisplayName = profile.PreferredUserName;

                        if ((user.DisplayName += "").Trim() == string.Empty)
                            user.DisplayName = "Uknown";
                    }
                    if(string.IsNullOrEmpty(user.Location))
                    {
                        if (profile.Address != null)
                            user.Location = (profile.Address.Locality + " " + profile.Address.Region).Trim();
                    }
                    user.BirthDate = user.BirthDate ?? profile.Birthday;
                    user.Photo = (string.IsNullOrEmpty(user.Photo) ? profile.Photo : user.Photo) + "";
                    user.PreferredUserName = profile.PreferredUserName + "";
                    user.LastVisit = DateTime.Now;

                    using (var tx = _repository.BeginTransaction())
                    {
                        _repository.Save(user);
                        _repository.Save(new ActionItem{Action = ActionType.LoggedIn, DateCreated = DateTime.Now, UserId = user.Id});
                        tx.Commit();
                    }

                    return user;
                }

                return CreateUserFromProfile(profile);
            }
        }