Roles and User Profiles

nitavn

New Member
I am developing a website and need some additional information that goes beyond the basic CreateUserWizard in asp.net 4.0. I need to store first and last names, as well as a company name for the user when they create an account, then assign the user to one of fourroles (manager, user_sf, user_tl and user). I have added a step in the beginning of the wizard to collect the first, last and company name of the user and a dropdown selection that will determine which role they are assigned to. I have no problems creating the user, but it never assigns them to a specific role, it always assigns them to the "user" role for whatever reason. I want to collect the first and last names so if someone gets fired from a company that orders from our website, we can remove their account based on their ACTUAL name, not their username (because we all know how whacky those can be!). Here is some code that I have so far that works, but does not give me the desired results, and I am not sure if I'm storing the first, last and company names correct or not.\[code\]protected void RegisterUser_CreatingUser(object sender, LoginCancelEventArgs e) { try { string trimUserName = RegisterUser.UserName.Trim(); if (RegisterUser.UserName.Length != trimUserName.Length) { accountlbl.Text = "The username cannot contain leading or trailing spaces."; accountlbl.Visible = true; //Cancel the created user info e.Cancel = true; } if (RegisterUser.Password.IndexOf(RegisterUser.UserName, StringComparison.OrdinalIgnoreCase) > 0) { accountlbl.Text = "The username may not appear anywhere in the password."; accountlbl.Visible = true; //Cancel the created user info e.Cancel = true; } } catch { accountlbl.Text = "An unknown error has occured. Please try again."; } } protected void RegisterUser_NextButtonClick(object sender, WizardNavigationEventArgs e) { partslist = companyddl.SelectedValue.ToString(); } protected void RegisterUser_FinishButtonClick(object sender, WizardNavigationEventArgs e) { try { MembershipCreateStatus createStatus = new MembershipCreateStatus(); MembershipUser newUser = System.Web.Security.Membership.CreateUser(RegisterUser.UserName, RegisterUser.Password, RegisterUser.Email, RegisterUser.Question, RegisterUser.Answer, true, out createStatus); //Assign Users to their Role if (partslist == "SF") Roles.AddUserToRole(RegisterUser.UserName, "user_sf"); if (partslist == "TL") Roles.AddUserToRole(RegisterUser.UserName, "user_tl"); if (partslist == "" && companytxt.Text != "") Roles.AddUserToRole(RegisterUser.UserName, "user"); if (partslist == "" && companytxt.Text == "Fapco" || companytxt.Text == "Fapco, Inc.") Roles.AddUserToRole(RegisterUser.UserName, "manager"); } catch { //error } } protected void RegisterUser_CreatedUser(object sender, EventArgs e) { try { //Assign Users to their Role //if (partslist == "SF") // Roles.AddUserToRole(RegisterUser.UserName, "user_sf"); //if (partslist == "TL") // Roles.AddUserToRole(RegisterUser.UserName, "user_tl"); //if (partslist == "" && companytxt.Text == "OurCompany" || companytxt.Text == "OurCompany, Inc.") // Roles.AddUserToRole(RegisterUser.UserName, "manager"); //Add additional Info to User Profile ProfileBase profile = ProfileBase.Create(RegisterUser.UserName); profile["FirstName"] = fnametxt.Text.ToString(); profile["LastName"] = lnametxt.Text.ToString(); profile["Company"] = companytxt.Text.ToString(); profile.Save(); //Email list with username, company and parts to order from } catch { //error } }\[/code\]In the web.config file:\[code\]<profile> <providers> <clear/> <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/> </providers> <properties> <add name="FirstName"/> <add name="LastName"/> <add name="Company"/> </properties></profile>\[/code\]
 
Back
Top