1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Create User Wizard Control and Login Control Programmatically

Create User Wizard Control and Login Control Programmatically

2 posts, 0 answered
  1. CafeDelMar
    CafeDelMar avatar
    28 posts
    Registered:
    06 May 2007
    20 Apr 2009
    Link to this post
    Dear All

    I have made two user control: Create User Wizard Control and Login Control

    CreateUserWizard Control code
    1 using System; 
    2 using System.Collections; 
    3 using System.Configuration; 
    4 using System.Data; 
    5 using System.Web; 
    6 using System.Web.Security; 
    7 using System.Web.UI; 
    8 using System.Web.UI.HtmlControls; 
    9 using System.Web.UI.WebControls; 
    10 using System.Web.UI.WebControls.WebParts; 
    11 using Telerik.Security; 
    12 using System.Web.Profile;    
    13  
    14 public partial class UserControls_CreateUserWizardControl : System.Web.UI.UserControl 
    15
    16     protected void Page_Load(object sender, EventArgs e) 
    17     { 
    18  
    19     } 
    20     protected void ibRegister_Click(object sender, ImageClickEventArgs e) 
    21     { 
    22         String UserName = rtbUserName.Text.Trim(); 
    23         String UserPassword = rtbUserPassword.Text.Trim(); 
    24         String UserEmail = rtbUserEmail.Text.Trim(); 
    25         String UserFirstName = rtbUserFirstName.Text.Trim();    
    26         String UserLastName = rtbUserLastName.Text.Trim(); 
    27         String UserCompany = rtbUserCompany.Text.Trim();   
    28         String UserPhonertbUserPhone.Text.Trim();    
    29         String UserCountry = rcbUserCountry.Text.Trim(); 
    30         bool UserEula = cbUserEula.Checked; 
    31         String RoleName = "everyone"
    32         try 
    33         { 
    34             MembershipUser User = Membership.CreateUser(UserName, UserPassword, UserEmail); 
    35             ProfileBase profile = ProfileBase.Create(UserName); 
    36             profile.SetPropertyValue("FirstName", UserFirstName); 
    37             profile.SetPropertyValue("LastName", UserLastName); 
    38             profile.SetPropertyValue("Company", UserCompany); 
    39             profile.SetPropertyValue("Phone", UserPhone); 
    40             profile.SetPropertyValue("Country", UserCountry); 
    41             profile.SetPropertyValue("Eula", UserEula); 
    42             Roles.AddUserToRole(UserName,RoleName);  
    43             if (profile.IsDirty) 
    44                 profile.Save();  
    45  
    46         } 
    47         catch(MembershipCreateUserException ex) 
    48         { 
    49             lRegisterErrMsg.Visible = true
    50             lRegisterErrMsg.Text = GetErrorMessage(ex.StatusCode);  
    51         } 
    52     } 
    53  
    54     public string GetErrorMessage(MembershipCreateStatus status) 
    55     { 
    56         switch (status) 
    57         { 
    58             case MembershipCreateStatus.DuplicateUserName: 
    59                 return "Username already exists. Please enter a different user name."; 
    60  
    61             case MembershipCreateStatus.DuplicateEmail: 
    62                 return "A username for that e-mail address already exists. Please enter a different e-mail address."; 
    63  
    64             case MembershipCreateStatus.InvalidPassword: 
    65                 return "The password provided is invalid. Please enter a valid password value."; 
    66  
    67             case MembershipCreateStatus.InvalidEmail: 
    68                 return "The e-mail address provided is invalid. Please check the value and try again."; 
    69  
    70             case MembershipCreateStatus.InvalidAnswer: 
    71                 return "The password retrieval answer provided is invalid. Please check the value and try again."; 
    72  
    73             case MembershipCreateStatus.InvalidQuestion: 
    74                 return "The password retrieval question provided is invalid. Please check the value and try again."; 
    75  
    76             case MembershipCreateStatus.InvalidUserName: 
    77                 return "The user name provided is invalid. Please check the value and try again."; 
    78  
    79             case MembershipCreateStatus.ProviderError: 
    80                 return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator."; 
    81  
    82             case MembershipCreateStatus.UserRejected: 
    83                 return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator."; 
    84  
    85             default: 
    86                 return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator."; 
    87         } 
    88     } 
    89
    90  

    Login Control code
    1 using System; 
    2 using System.Collections; 
    3 using System.Configuration; 
    4 using System.Data; 
    5 using System.Web; 
    6 using System.Web.Security; 
    7 using System.Web.UI; 
    8 using System.Web.UI.HtmlControls; 
    9 using System.Web.UI.WebControls; 
    10 using System.Web.UI.WebControls.WebParts; 
    11
    12 public partial class UserControls_LoginControl : System.Web.UI.UserControl 
    13
    14     protected void Page_Load(object sender, EventArgs e) 
    15     { 
    16  
    17     } 
    18     protected void ibLogin_Click(object sender, ImageClickEventArgs e) 
    19     { 
    20         if (FormsAuthentication.Authenticate(rtbUserID.Text.Trim(), rtbPass.Text.Trim())) 
    21         { 
    22             FormsAuthentication.SetAuthCookie(rtbUserID.Text.Trim(), cbRememberMe.Checked); 
    23             Response.Redirect("home.aspx"); 
    24         } 
    25         else 
    26         { 
    27             lLoginErrMsg.Visible = true
    28             lLoginErrMsg.Text = "Invalid Login!";   
    29             Response.Redirect("index.aspx"); 
    30         } 
    31     } 
    32
    33  


    then several problem appears
    1. i already created new user successfully, but new user can't login, do i miss something with the code?

    2. the error message doesn't fire in lLoginErrMsg.text & lRegisterErrMsg

    Thanks
     regards

    febry
     



  2. Parvan
    Parvan avatar
    151 posts
    Registered:
    24 Sep 2012
    21 Apr 2009
    Link to this post
    Hi CafeDelMar,

    1. You need to use Membership.ValidateUser instead of FormAuthentication.Authenticate method:
            protected void ibLogin_Click(object sender, ImageClickEventArgs e)  
            { 
                if (Membership.ValidateUser(rtbUserID.Text.Trim(), rtbPass.Text.Trim())
                {  
                    FormsAuthentication.SetAuthCookie(rtbUserID.Text.Trim(), cbRememberMe.Checked);  
                    Response.Redirect("home.aspx");  
                }  
                else  
                {  
                    lLoginErrMsg.Visible = true;  
                    lLoginErrMsg.Text = "Invalid Login!";    
                    Response.Redirect("index.aspx");  
                }  
            }  

    2. Because the *.ascx markup was not supplied, I suppose that lLoginErrMsg and lRegisterErrMsg are Label controls. The lLoginErrMsg will not be seen because you redirect to index.aspx after setting its Text property. The lRegisterErrMsg will be seen by an user when a MembershipCreateUserException exception is thrown.

    Greetings,
    Parvan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
2 posts, 0 answered