More in this section

Forums / General Discussions / Create User Wizard - Sitefinity 4

Create User Wizard - Sitefinity 4

11 posts, 0 answered
  1. Joseph
    Joseph avatar
    11 posts
    Registered:
    03 Aug 2010
    12 Dec 2010
    Link to this post
    Hi,

    Since Sitefinity 4 uses a custom security API, the create user wizard control needs to be updated. I would like to post my working code for the Create User Wizard using Sitefinity 4:

    CreateUserWizard.ascx:

    <asp:CreateUserWizard ID="CreateUserWizard1"  runat="server"  CreateUserButtonText="Sign up"  CreateUserButtonStyle-CssClass="test" >
    <CreateUserButtonStyle CssClass="test"></CreateUserButtonStyle>
        <WizardSteps>
            <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
                <ContentTemplate>
                    <table>
                    <tr>
                    <td>
                     <asp:Label AssociatedControlID="FirstName" ID="FirstNameLabel" runat="server" Text="First name <em>(optional)</em> :" />
                     </td><td>
                        <asp:TextBox ID="FirstName" runat="server" Width="180px" />
                        <asp:RequiredFieldValidator ID="FirstNameRequired" runat="server" ControlToValidate="FirstName"
                                    ErrorMessage="FirstName is required." ToolTip="FirstName is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                    </td>
                    </tr>
                    <tr>
                    <td>
                     <asp:Label AssociatedControlID="LastName" ID="LastNameLabel" runat="server" Text="Last name <em>(optional)</em> :" /></td>
                     <td>
                        <asp:TextBox ID="LastName" runat="server" Width="180px" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="LastName"
                                    ErrorMessage="LastName is required." ToolTip="LastName is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                    </td>
                    </tr>
                       <tr>
                       <td>
                                <asp:Label ID="UserNameLabel" runat="server" Width="180px" AssociatedControlID="UserName">User name:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="UserName" runat="server" Width="180px"></asp:TextBox>
                           </td>
                            </tr>
                            <tr>
                            <td>
                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                            </td>
                                <td>
                                    <asp:TextBox ID="Password" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server"
                                        ControlToValidate="Password" ErrorMessage="Password is required."
                                        ToolTip="Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr>
                            <td>
                                <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm password:</asp:Label>
                            </td>
                                <td>
                                    <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"
                                        Width="180px"></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server"
                                        ControlToValidate="ConfirmPassword"
                                        ErrorMessage="Confirm Password is required."
                                        ToolTip="Confirm Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr>
                            <td>
                                <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question" Width="180px">Security question:</asp:Label>
                            </td>
                                <td>
                                    <asp:TextBox ID="Question" runat="server" Width="180px"></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="QuestionRequired" runat="server"
                                        ControlToValidate="Question" ErrorMessage="Security question is required."
                                        ToolTip="Security question is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr>
                            <td>
                                <asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer" Width="180px">Security answer:</asp:Label>
                             </td>
                                <td>
                                    <asp:TextBox ID="Answer" runat="server" Width="180px"></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="AnswerRequired" runat="server"
                                        ControlToValidate="Answer" ErrorMessage="Security answer is required."
                                        ToolTip="Security answer is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                </td>
                             </tr>
                             <tr>
                             <td>
                                <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email" Width="180px">Email:</asp:Label>
                             </td>
                                 <td>
                                     <asp:TextBox ID="Email" runat="server" Width="180px"></asp:TextBox>
                                     <asp:RequiredFieldValidator ID="EmailRequired" runat="server"
                                         ControlToValidate="Email" ErrorMessage="E-mail is required."
                                         ToolTip="E-mail is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                 </td>
                             </tr>
                        <caption>
                            <p>
                                <asp:CompareValidator ID="PasswordCompare" runat="server"
                                    ControlToCompare="Password" ControlToValidate="ConfirmPassword"
                                    Display="Dynamic"
                                    ErrorMessage="The Password and Confirmation Password must match."
                                    ValidationGroup="CreateUserWizard1"></asp:CompareValidator>
                            </p>
                            <p class="errorMsg">
                                <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                            </p>
                        </caption>
                   </table>
                </ContentTemplate>
                <CustomNavigationTemplate>
                <div id="stepnextbuttondiv">
                <p class="sfSubmitBtn sfMainFormBtns"
                 <asp:LinkButton ID="StepNextButton" runat="server" CssClass="sfLinkBtnIn"
                         CommandName="MoveNext" Text="Create User"
                         ValidationGroup="CreateUserWizard1" /></p></div>
                </CustomNavigationTemplate>
            </asp:CreateUserWizardStep>
             
            <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                <ContentTemplate>
                    <p><strong>Your account has been successfully created.</strong></p>
                    <p><asp:Button ID="ContinueButton" runat="server" CausesValidation="False" CommandName="Continue"
                        Text="Continue" ValidationGroup="CreateUserWizard1" /></p>
              
                </ContentTemplate>
            </asp:CompleteWizardStep>
        </WizardSteps>
    </asp:CreateUserWizard>

    CreateUserWizard.ascx.cs:

    using System;
    using System.Linq;
    using System.Web.UI.WebControls;
    using Telerik.Sitefinity.Security;
     
    namespace SitefinityWebApp.UserControls.Login
    {
        public partial class CreateUserWizardControl : System.Web.UI.UserControl
        {
            //Instantate the manager objects, so I can disable security checks
            UserManager manager = UserManager.GetManager("Default");
            RoleManager Rmanager = RoleManager.GetManager("Default");
     
            public void Page_Load(object sender, EventArgs e)
            {
                //Suppress all security checks. This ensures that unauthenticated users can create user accounts
                manager.Provider.SuppressSecurityChecks = true;
                Rmanager.Provider.SuppressSecurityChecks = true;
                //Call the created user event because this control uses Sitefinity API for security, instead of .NET provider
                this.CreateUserWizard1.CreatedUser +=new EventHandler(CreateUserWizard1_CreatedUser);
            }
           
            void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
            {
                //Create the role, if it does not exist
                if (!Rmanager.RoleExists("RegisteredUsers"))
                {
                    Rmanager.CreateRole("RegisteredUsers");
                }
     
                //Get first name and last name variables
                TextBox FirstName = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("FirstName");
                TextBox LastName = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("LastName");
     
                //get the user
                var user = manager.GetUserByEmail(CreateUserWizard1.Email);
     
                //Add the user to the role
                Rmanager.AddUserToRole(user, Rmanager.GetRoles().Where(q => q.Name == "RegisteredUsers").First());
     
                //Don't forget to save changes
                Rmanager.SaveChanges();
                 
                //Update the user's information
                user.FirstName = FirstName.Text;
                user.LastName = LastName.Text;
     
                //Save the changes
                manager.SaveChanges();
            }
        }
    }
  2. Joe
    Joe avatar
    40 posts
    Registered:
    11 Sep 2007
    13 Dec 2010
    Link to this post
    Thanks!
  3. John
    John avatar
    97 posts
    Registered:
    11 Jan 2011
    19 Jan 2011
    Link to this post
    Hi Joseph

    Many thanks for this. I am having problem at the last line (line 14) in below code;

    01.public const string ApplicantUserRole = "Applicant";
    02.UserManager manager = UserManager.GetManager("Default");
    03.RoleManager Rmanager = RoleManager.GetManager("Default");
    04.void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    05.{
    06.    //Create the role, if it does not exist
    07.    if (!Rmanager.RoleExists(ApplicantUserRole))
    08.    {
    09.        Rmanager.CreateRole(ApplicantUserRole);
    10.    }
    11.    //get the user
    12.    var user = manager.GetUserByEmail(CreateUserWizard1.Email);
    13.    //Add the user to the role
    14.    Rmanager.AddUserToRole(user, Rmanager.GetRoles().Where(q => q.Name == ApplicantUserRole).First());
    15.}

    The error I'm getting is;

    Exception Details: System.InvalidOperationException: No items found in the sequence.

    I suspect the role "Applicant" is not being created.

    Any ideas how to fix this?

    Thanks

    Regards

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    19 Jan 2011
    Link to this post
    Hi John01,

    You use First(), but your query does not return items which results in the error you see.
    You can use FirstOrDefault instead.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. John
    John avatar
    97 posts
    Registered:
    11 Jan 2011
    19 Jan 2011
    Link to this post
    Hi

    I have checked in backend and role "Applicant" is not created. What is the correct code to create roles as;

    public const string ApplicantUserRole = "Applicant";
    RoleManager Rmanager
    = RoleManager.GetManager("Default");

    if (!Rmanager.RoleExists(ApplicantUserRole))
    {   
      Rmanager.CreateRole(ApplicantUserRole);
    }


    did not seem to have worked?

    Thanks

    Regards

  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    19 Jan 2011
    Link to this post
    Hi,

    You have to call SaveChanges of the manager.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. John
    John avatar
    97 posts
    Registered:
    11 Jan 2011
    19 Jan 2011
    Link to this post

    Hi

    Many thanks.

    I need two more features please;

    a. I need to add additional fields to Create User Wizard. How does that work with SF provider? Where and how do the additional fields get stored?

    b. How do I make the created users have access to some pages but not to others?

    Thanks

    Regards

  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    19 Jan 2011
    Link to this post
    Hi Joseph,

    a. I need to add additional fields to Create User Wizard. How does that work with SF provider? Where and how do the additional fields get stored?

    Create User Wizard uses the Membership Provider. If you want to use additional fields you can use Profile provider represented by ProfileManager class.

    b. How do I make the created users have access to some pages but not to others?

    You can set permissions per page from the UI.


    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    07 Feb 2011
    Link to this post
    Hi all

    Finally getting to look at SF4 for a project.

    Looking for a new user registration control. Do I really need to create it myself? Amazed that this in not included in the release or am I missing something?

    M
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    09 Feb 2011
    Link to this post
    Hi mattc,

    There are no plans for a new registration control for the Q1 release. We will consider adding our own control after that. As you have already noticed CreateUserWizard will do the trick.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  11. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    15 Feb 2011
    Link to this post
    Hi Ivan

    Yes fair enough, the CreateUserWizard works pretty smoothly :)

    Thanks

    Matt
11 posts, 0 answered