Null reference error using the authentication service

Topics: Writing modules
May 24, 2011 at 6:49 PM
Edited May 24, 2011 at 8:33 PM

-- Title of post was updated to reflect topic discussion --

We have a need to put a user in a specified role upon registering via a custom url. So, my idea was to copy the necessary pieces of code from the existing user module and create a new registration module, so that when you go to myorchardsite.foo/noob/register it loads a form identical to that of the register page built-in to orchard.

I've gotten as far as creating a new controller/view and route class so that the url routes the user correctly.  My first goal is to just make it create the user as the built-in module does.  When I get that working, I'll tackle the similarly difficult task of finding out how to tie a use to a role using the API.

The problem occurs when I'm using the membership API.  I can get all the way to the point where I call _authenticateService.SignIn(user, false /*createPersistentCookie */); and on that line, I get a null reference exception in ShellRoute.cs on line 148.  I am unable to debug into the SignIn method to see why the null reference is happening, and with a lack of documentation on the orchard API, I'm struggling to get get through this task by simply eyeballing the existing user module.


Can anyone assist in getting me past this issue?

Here's my code (the line in red is where I get the error):

public ActionResult Register(string userName, string email, string password, string confirmPassword)
        {
            // ensure users can register
            var registrationSettings = _orchardServices.WorkContext.CurrentSite.As<RegistrationSettingsPart>();
            if (!registrationSettings.UsersCanRegister)
            {
                return HttpNotFound();
            }

            ViewData["PasswordLength"] = MinPasswordLength;

            if (ValidateRegistration(userName, email, password, confirmPassword))
            {
                // Attempt to register the user
                // No need to report this to IUserEventHandler because _membershipService does that for us
                var user = _membershipService.CreateUser(new CreateUserParams(userName, password, email, null, null, false));

                if (user != null)
                {
                    if (user.As<UserPart>().EmailStatus == UserStatus.Pending)
                    {
                        _userService.SendChallengeEmail(user.As<UserPart>(), nonce => Url.AbsoluteAction(() => Url.Action("ChallengeEmail", "Account", new { Area = "Orchard.Users", nonce = nonce })));

                        foreach (var userEventHandler in _userEventHandlers)
                        {
                            userEventHandler.SentChallengeEmail(user);
                        }
                        return RedirectToAction("ChallengeEmailSent");
                    }

                    if (user.As<UserPart>().RegistrationStatus == UserStatus.Pending)
                    {
                        return RedirectToAction("RegistrationPending");
                    }

                    _authenticationService.SignIn(user, false /* createPersistentCookie */);
                    return Redirect("~/");
                }

                ModelState.AddModelError("_FORM", T(ErrorCodeToString(/*createStatus*/MembershipCreateStatus.ProviderError)));
            }
Coordinator
May 24, 2011 at 7:11 PM

Why are you unable to debug into it?

May 24, 2011 at 7:43 PM
Edited May 24, 2011 at 7:44 PM

I'm not entirely sure...I step through the code, and when the breakpoint is on the red line above, I hit F11 to step into it and it.  It doesn't step in, it just throws the NullReferenceException, so I'm not able to find out where this is happening.

Coordinator
May 24, 2011 at 7:44 PM

Do you have a full source code enlistment? Did you build debug?

May 24, 2011 at 7:49 PM

I did build in debug mode...we downloaded the 17MB source version from the orchardproject.net homepage:

http://orchard.codeplex.com/releases/view/59918#DownloadId=227335

May 24, 2011 at 7:50 PM

It seems like I"m able to step through other parts of the code, it just doesn't want to step into the authenticationService code...

Coordinator
May 24, 2011 at 7:54 PM

And it's not the authentication service that's null, right?

May 24, 2011 at 8:02 PM
Edited May 24, 2011 at 8:03 PM

Man. I created the private member but never set it in the constructor.

Might we be able to delete this entire post out of sheer embarrassment? I promise you I moused over every single one of those items in debug mode to check if they were null, and I must have skipped past the most obvious one. That definitely explains why I wasn't able to debug in. 

God, I feel like such a noob.  Maybe I should visit myorchardsite.foo/noob/register in my browser :)

Thank you for your prompt responses...

Coordinator
May 24, 2011 at 8:04 PM

These things happen even to the best of us. Seriously, do you want me to delete the thread? It could help other people who make the same sort of mistake.

May 24, 2011 at 8:10 PM

Nah, I think it's a good idea to keep it active in case it could help someone else. Thanks again...