1

I am trying to create a User Profile when an anonymous user registers on my FBA web application. The below CreateUserProfile() method is being called during the register event. It works well in SharePoint 2010, but is throwing an error in SharePoint 2013.

I am getting an error "Object Reference not set to instance of the object" on userProfileManager.UserExists() method. I checked the Non-Public Members of userProfileManager object and found that 'userProfileManager.strCurrentAccountName' threw an exception of type 'System.NullReferenceException'. Why this behaviour in SharePoint 2013?

    private void CreateUserProfile()
    {
        SPSite currentSiteT = SPContext.Current.Site;
        SPWeb currentWebT = currentSiteT.OpenWeb();
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite(currentSiteT.ID))
            {
                using (SPWeb web = site.OpenWeb(currentWebT.ID))
                {                  
                    httpCtx = HttpContext.Current;
                    HttpContext.Current = null;                  
                    try
                    {                            
                        serviceContext = SPServiceContext.GetContext(site);                       
                        userProfileManager = new UserProfileManager(serviceContext);
                        bool existingUser = userProfileManager.UserExists("i:0#.f|FBAMembershipProvider|" + this.txtEmail.Text);
                    }
               }    
          }
      } 
   }  
0

I have not yet registered users in SharePoint 2013. But in SP2010 used to below code for registering the users in FBA, kindly refer if it can be useful to you.

                MembershipCreateStatus mcs = new MembershipCreateStatus();
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                    using (SPWeb spWeb = site.OpenWeb())
                    {
                        try
                        {
                            MembershipUser membershipUser = Membership.Providers["FBAMembershipProvider"].CreateUser(txtUsername.Text, txtPassword.Text, txtEmail.Text, txtquestion.Text, txtanswer.Text, true, null, out mcs);
                            spWeb.AllowUnsafeUpdates = true;
                            SPUser spUser = spWeb.EnsureUser(txtUsername.Text);
                            SPGroup spGroup = spWeb.Groups[ddCategory.SelectedItem.Text];
                            spGroup.AddUser(spUser);
                            spGroup.Update();
                            spWeb.AllowUnsafeUpdates = false;
                        }
                        catch (Exception ex)
                        {
                            Logger.ExceptionLog("Error"); 
                            System.Console.WriteLine(ex.ToString());
                        }
                    }
                });
0

Late to the party here but we have just had this exact problem. In fact more so since we had it working on local VM environments but it failed on deployment. After several hours of teeth gnashing and hair pulling we have a solution. Well a workaround really but it works for us within the scope of our requirements.

We surmised that it HAD to be some kind of permissions issue. So to circumvent the user context being anonymous, we logged the FBA user in and then ran the code to create the user profile. This worked. The user profile was successfully created.

The code for this is something like

Microsoft.SharePoint.IdentityModel.SPClaimsUtility.AuthenticateFormsUser(new Uri(spweb.Url), userName, password);

Of course this might not be an option for you but perhaps you could always sign the user back out once the profile is created successfully?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.