Sharepoint 2010. Create user using FBA

3.2k views Asked by At

I'm trying to create user in SharePoint 2010 using FBA (Forms Based Authentication). I followed the steps in this article to configure FBA: http://donalconlon.wordpress.com/2010/02/23/configuring-forms-base-authentication-for-sharepoint-2010-using-iis7/

Then I uploaded the FBA package http://sharepoint2010fba.codeplex.com/. Web part loaded normally, but the user is not created. If you try to create a user it gives an unknown error.

After that I tried to create authorization form by myself. The code below handles the registration event:

    MembershipCreateStatus mcs = new MembershipCreateStatus();
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
       using (SPSite site = new SPSite("http://makovkasp:30763/"))
       using (SPWeb spWeb = site.OpenWeb())
       { 
          try
          {

             MembershipUser membershipUser =
Membership.Providers["FBAMembershipProvider"].CreateUser(txtLogin.Text,
                                   txtPassword.Text,
                                   txtEmail.Text,
                                   txtSecretQuestion.Text,
                                   txtAnswer.Text, true, null, out mcs);

             spWeb.AllowUnsafeUpdates = true;
             SPUser spUser = spWeb.EnsureUser(txtLogin.Text);
             SPGroup spGroup = spWeb.Groups[2];
             spGroup.AddUser(spUser);
             spGroup.Update();

          }
          catch (Exception ex)
          {
             System.Console.WriteLine(ex.ToString());
          }
       }
    });

After this code is executed, the user creates in SharePoint (I can see it in FBA UserManagement). But it isn't created in FBA database. So, I can't sign in under it's account. Anyone knows how to do it?

Thanks.

3

There are 3 answers

0
Chris Coulson On

The unknown error when registering a user generally means there was an error sending the email. You can check the SharePoint ULS logs for more details. I would check your SharePoint outgoing mail settings.

If you can see the user in FBA UserManagement - then it has been created in the membership database. Have you checked the aspnetdb database to see if the user exists?

Can you login with another FBA user's account? If not it's likely there's a problem with your FBA setup. Make sure the membership entries are in your STS and Central Admin web configs.

2
dariom On

You should interrogate the mcs parameter value after your code is executed. It might indicate why the user account was not created. The MembershipCreateStatus enumeration should show why the user was not saved successfully by the membership provider.

0
DDev On

This should probably be a comment, but I don't have the rep to do that yet. This is for anyone who found this question because they were confused about all the other answers that just say to use

Membership.CreateUser() 

to create users in an FBA-enabled SharePoint site.

When you have a web application with multiple authentication providers enabled, you need to specify the provider using:

Membership.Providers[MembershipProvider].CreateUser()

To get the MembershipProvider, you can specify it as a hard-coded string like OP did, or you can get it dynamically using:

SPSite.WebApplication.IisSettings[SPUrlZone].FormsClaimsAuthenticationProvider.MembershipProvider()