コード例 #1
0
        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;
        }
コード例 #2
0
        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;
        }