Learn more about SQL Server tools

mssqltips logo
 

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories

 

Register New SharePoint User with Custom Page in SharePoint 2010 FBA Authentication


By:   |   Read Comments   |   Related Tips: > Sharepoint

Attend this free live MSSQLTips webcast

Tips and Tricks to Growing Your SQL Server Estate from SMB to Enterprise Scale
Thursday, June 28, 2018 - click here to learn more


Problem

Many SharePoint sites need to allow public access and/or registration. However developers face the problem of programmatically adding a new user to needed groups in order to avoid "Access Denied" errors.

Solution

This article will demonstrate how to register a new user programmatically for a SharePoint site using a custom register page and add this user into a SharePoint user group. This solution is an enhanced version of the tip on "Creating and deploying custom Login page - SharePoint 2010 FBA authentication". 

Step 1:

(This first step is same as step 1 from the above referenced tip. If you already created the project from that step and would like to reuse it, you may skip to Step 2 below.)

  • Create a new project of type "Empty SharePoint Project" in Visual Studio 2010 as shown below.
new project
  • Provide a Project name and Press "OK". You will be prompted by a Wizard to specify a SharePoint site where you want to deploy a custom login page. You can validate the site URL to ensure that your site is fine to deploy in this solution. Remember that you have to choose Deploy as Server Farm solution instead of deploying as a sandboxed solution (default selected as below). Otherwise you will get an error when you deploy the solution "The deployment type "TemplateFile" of file "CustomLogin.aspx" in Project Item "Layouts" is not compatible with a Package in a Sandboxed Solution" Because Layout is the root place to deploy in SharePoint server farm which is mapped {SharePointRoot}\Template\Layouts.

So you will select Deploy as Server Farm solution after validation of the site URL.

sharepoint customization wizard
  • Then "Click to Finish" to create the project.

Step 2:

You have to add a new Application Page using a right-click on the project Solution Explorer -> New item to create a Register Page (Register.aspx) in the following way.

online templates

The form will auto-generate some code where you:

  • Need to remove the entry asp:Content ID="Content1" runat="server" from the Register.aspx
  • Need to remove DynamicMasterPageFile="~masterurl/default.master" from @Page Register tag
  • client objects
  • Change Inheritance LayoutsPageBase to System.Web.UI.Page
  • Copy the following code to register the new user. Detailed steps are:
    • Create membership user
    • Create SharePoint user
    • Add SharePoint user to a desired group
string login = UserName.Text.Trim();
string password = Password.Text.Trim();
string email = Email.Text.Trim();

//1) Create membership user
MembershipUser membershipUser = Membership.CreateUser(login, password, email);
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate
{
    //2) Create sharepoint user
    using (SPSite spSite = new SPSite(SPContext.Current.Site.ID))
    {
        SPWeb spWeb = spSite.OpenWeb();
        spWeb.AllowUnsafeUpdates = true;
        SPUser spUser = spWeb.EnsureUser(login);

        //3) Add sharepoint user to a desired group
        SPGroup spGroup = spWeb.SiteGroups["FBA Demo Members"];
        spGroup.AddUser(spUser);
        spWeb.AllowUnsafeUpdates = false;
    }
});

Step 3:

Then deploy the solution which will create a folder inside .\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS\CustomLoginPageFBA 

Step 4:

  • Go to Central Administration
  • Select "Site" from "Application Management"
  • Click on Authentication Provider
  • Select the Zone and go inside it to get the following Sign In Page URL Section
  • Change the Custom Sign In Page URL from where you will link Register Page
sign in page
Next Steps
  • Download the complete source code from here
  • Return to MSSharepointTips to read about other topics and ideas.
  • Check out MSSQLTips.com for great information about Microsoft SQL Server


Last Update:


next webcast button


next tip button



About the author





Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Email me updates 


Signup for our newsletter
 I agree by submitting my data to receive communications, account updates and/or special offers about SQL Server from MSSQLTips and/or its Sponsors. I have read the privacy statement and understand I may unsubscribe at any time.



    



Learn more about SQL Server tools