3

我的 DotNetNuke 模块中有以下代码来注册新用户:

protected static int RegisterNewDnnAccount(string firstName, string lastName, string email, string password)
    {
        // Create user
        var user = new UserInfo();
        user.FirstName = firstName;
        user.LastName = lastName;
        user.Email = email;
        user.DisplayName = string.Concat(firstName, " ", lastName);
        user.Username = email;
        user.IsSuperUser = false;

        user.Profile.FirstName = firstName;
        user.Profile.LastName = lastName;
        user.Profile.PreferredTimeZone = TimeZoneInfo.Local;
        user.Profile.PreferredLocale = "en-US";
        user.Profile.Telephone = string.Empty;
        user.Profile.Cell = string.Empty;
        user.Profile.PostalCode = string.Empty;
        user.Profile.City = string.Empty;
        user.Profile.Street = string.Empty;
        user.Profile.Region = string.Empty;

        // Set up membership
        var membership = new UserMembership(user);
        membership.Approved = true;
        membership.CreatedDate = DateTime.Now;
        membership.Password = password;
        membership.IsOnLine = false;
        user.Membership = membership;

        // Register the account
        var status = UserController.CreateUser(ref user);
        switch (status)
        {
            case UserCreateStatus.Success:
                return user.UserID;

            case UserCreateStatus.InvalidPassword:
                throw new InvalidOperationException("Failed to create user account: invalid password");

            case UserCreateStatus.InvalidEmail:
                throw new InvalidOperationException("Failed to create user account: Invalid email");

            case UserCreateStatus.DuplicateEmail:
                throw new InvalidOperationException("A user already exists with this email address");

            case UserCreateStatus.UserAlreadyRegistered:
                throw new InvalidOperationException("This user is already registered");

            default:
                throw new InvalidOperationException("user account creation failed for an unknown reason");
        }
    }

当该UserController.CreateUser(ref user);方法被调用时,DotNetNuke 的 API 会抛出一个NullReferenceException,当然关于什么是 null 的信息为零。

我已经验证了我在上面设置的每个字段都不为空,所以很明显这是我没有设置的东西导致了这个问题。我设置的大多数属性都是我试图让这个错误停止,但我没有做到这一点。

有任何想法吗?


编辑:这是堆栈跟踪:

在C DotNetNuke.Entities.Portals.PortalController.IsMemberOfPortalGroup(Int32 portalId)
: \XOS\Projects\DNN\Website\DesktopModules\ DotNetNuke.Entities.Users.UserController.AutoAssignUsersToRoles(UserInfo user, Int32 portalId)
XOSDigital.PlayerOnDemand \ DotNetNuke.Entities.Users.UserController.CreateUser(UserInfo& user)
PlayerOnDemand.Common PlayerOnDemand.Common.Utils.ClientUserUtils.RegisterNewDnnAccount(String firstName, String lastName, String email, String password)\Utils\ClientUserUtils.cs:line 160

4

2 回答 2

1

弄清楚了。问题是我没有明确设置用户的门户。

于 2012-04-13T14:00:45.470 回答
0

DNN 中的“对象未设置”和堆栈跟踪中的任何异常都显示它是 portalSettings 或可能类似于语言的东西,通常是因为在调用的上下文中,没有将 portalSettings 对象加载到当前 context.items 集合中.

当人们在没有加载当前请求上下文的预定呼叫中生成电子邮件或执行其他工作时,通常会发生这种情况。

由于此问题,您可能必须在假上下文中模拟当前的 portalSettings 对象,才能使用 DNN Api 的某些部分。

于 2012-04-16T03:41:47.293 回答