using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class CreateAccount : System.Web.UI.Page { private void populateCombo(SqlDataSource ds, DropDownList comboName, string spName, string dataValueField, string dataTextField, string paramName, string paramValue) { ds.SelectCommandType = SqlDataSourceCommandType.StoredProcedure; ds.SelectCommand = spName; ds.SelectParameters.Clear(); // Declare a parameter & add it to the datasource //Parameter spParameter = new Parameter(paramName, TypeCode.Int32); //spParameter.DefaultValue = paramValue.ToString(); //ds.SelectParameters.Add(spParameter); // OR add the parameter directly ds.SelectParameters.Add(paramName, paramValue.ToString()); ds.SelectParameters[0].Direction = ParameterDirection.Input; comboName.DataSource = ds; comboName.DataValueField = dataValueField; comboName.DataTextField = dataTextField; comboName.DataBind(); comboName.Items.Insert(0, "Select One"); } protected void Page_Load(object sender, EventArgs e) { currTimeStampLabel.Text = "Today is " + Utilities.GetFormattedDateTime(); //lblHeader.Text = "You are logged in as " + Utilities.GetLoggedInUserID(User.Identity.Name); if (!Page.IsPostBack) { // Get Windows ID of user string logonUserName = Utilities.GetLoggedInUserID(User.Identity.Name); lblHeader.Text = "You are logged in as " + logonUserName; // Just to be safe, check if user exists MembershipUser user = Membership.GetUser(logonUserName); if (user != null) { if (Roles.IsUserInRole(logonUserName, "AdminPlanning")) { SqlDataSource ds = new SqlDataSource(); ds.ConnectionString = ConfigurationManager.ConnectionStrings["cnSqlRoleManager"].ToString(); ds.DataSourceMode = SqlDataSourceMode.DataSet; // Populate drop-downs populateCombo(ds, rolesDropDown, "aspnet_Roles_GetAllRoles", "RoleName", "RoleName", "ApplicationName", "/"); // Display form to collect email address collectEmailDiv.Style["display"] = "block"; userNameTextBox.Focus(); } else { // Their account exists but not in role "AdminPlanning", so redirect to homepage Response.Redirect("Default.html", true); } } else { // Their account doesn't exists, so redirect to homepage Response.Redirect("Default.html", true); } } } protected void submitButton_Click(object sender, EventArgs e) { // Get new Windows ID of user to create string logonUserName = userNameTextBox.Text; bool accountCreated = false; // Just to be safe, check if user exists again MembershipUser user = Membership.GetUser(logonUserName); if (user == null) { MembershipCreateStatus status; //Use usename1! for the password; not going to be a problem because they don't use it user = Membership.CreateUser(logonUserName, logonUserName + "abcd1!", emailAddress1TextBox.Text, "q", "a", true, out status); //If the status is not a success, as set by the reference variable if (status != MembershipCreateStatus.Success) { throw new Exception("The creation failed due to the following status: " + status.ToString()); } else { accountCreated = true; } } //Add user to the role if (rolesDropDown.SelectedItem.ToString().ToUpper() != "SELECT ONE") { if (Roles.IsUserInRole(logonUserName, rolesDropDown.SelectedItem.ToString()) == false) { Roles.AddUserToRole(logonUserName, rolesDropDown.SelectedItem.ToString()); if (rolesDropDown.SelectedItem.ToString() != "ChairBoardPortfolio") { emailAddress1TextBox.Text = ""; emailAddress2TextBox.Text = ""; userNameTextBox.Text = ""; userNameTextBox.Focus(); } // Display message that account was created displayMessageLabel.Text = "Account " + logonUserName + " was "; if (accountCreated == true) { displayMessageLabel.Text += "created and "; } displayMessageLabel.Text += "added to role " + rolesDropDown.SelectedItem.ToString() + "."; } else { // user already exists in this role displayMessageLabel.Text = logonUserName + " already exists in role " + rolesDropDown.SelectedItem.ToString() + "."; //displayMessageLabel.Visible = true; } } else { // Display message that role needs to be selected displayMessageLabel.Text = "Please select a role for this user."; } } }