21 March 2013

Managing User Roles from Code Behind in ASP.NET and C#

 When I was developing a small web project few years back, I was looking for a way to manage the users by assigning role/s to specific user using GridView and DropDownList. One day, I saw a sample project written in VB. I studied it. I translated it to C#. I implemented it. And it worked well. Here's my sample implementation:



protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindUserDetails();
            BindRoles();
        }
    }

    // Bind User Names to Dropdownlist
    protected void BindUserDetails()
    {
        DDL.DataSource = Membership.GetAllUsers();
        DDL.DataTextField = "UserName";
        DDL.DataValueField = "UserName";
        DDL.DataBind();
        DDL.Items.Insert(0, new ListItem("-- Select User --", "0"));
    }

    // Bind Roles to Gridview
    protected void BindRoles()
    {
        GVRoles.DataSource = Roles.GetAllRoles();
        GVRoles.DataBind();
    }

    // This event is used to assign roles to particular user
    protected void btnAssign_Click(object sender, EventArgs e)
    {
        string userName = ddlUsers.SelectedItem.Text;
        string[] userRoles = Roles.GetRolesForUser(userName);
        string errorMessage = string.Empty;
        string successMessage = string.Empty;
        string roleName = string.Empty;
        int i = 0;
        int j = 0;
        foreach (GridViewRow gvrow in GVRoles.Rows)
        {
            CheckBox chk = (CheckBox)gvrow.FindControl("chkRole");
            Label lbl = (Label)gvrow.FindControl("lblRole");
            roleName = lbl.Text;
            if (chk.Checked)
            {
                int index = Array.IndexOf(userRoles, roleName);
                if (index == -1)
                {
                    Roles.AddUserToRole(userName, roleName);
                    successMessage += roleName + ", ";
                    j += 1;
                }
            }
            else
            {
                int index = Array.IndexOf(userRoles, roleName);
                if (index > -1)
                {
                    // Remove the user from the role
                    Roles.RemoveUserFromRole(userName, roleName);
                    errorMessage += roleName + ", ";
                    i += 1;
                }
            }
        }
        if (errorMessage != string.Empty)
        {
            if (i > 1)
            {
                lblError.Text = userName + " was removed from roles " + errorMessage.Substring(0, errorMessage.Length - 2);
            }
            else
            {
                lblError.Text = userName + " was removed from role " + errorMessage.Substring(0, errorMessage.Length - 2);
            }
            lblError.ForeColor = Color.Red;
        }
        else
        {
            lblError.Text = string.Empty;
        }
        if (successMessage != string.Empty)
        {
            if (j > 1)
            {
                lblSuccess.Text = successMessage.Substring(0, successMessage.Length - 2) + " roles are assigned to " + userName;
            }
            else
            {
                lblSuccess.Text = successMessage.Substring(0, successMessage.Length - 2) + " role is assigned to " + userName;
            }
            lblSuccess.ForeColor = Color.BlueViolet;
        }
        else
        {
            lblSuccess.Text = string.Empty;
        }
    }

    // This event is used to populate checkboxes based on roles for particular user
    protected void ddlUsers_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblSuccess.Text = string.Empty;
        lblError.Text = string.Empty;
        string userName = ddlUsers.SelectedItem.Text;
        string[] userRoles = Roles.GetRolesForUser(userName);
        string rolename = string.Empty;
        foreach (GridViewRow gvrow in gvRoles.Rows)
        {
            CheckBox chk = (CheckBox)gvrow.FindControl("chkRole");
            Label lbl = (Label)gvrow.FindControl("lblRole");
            rolename = lbl.Text;
            int index = Array.IndexOf(userRoles, rolename);
            if (index > -1)
            {
                chk.Checked = true;
            }
            else
            {
                chk.Checked = false;
            }
        }
    }

No comments:

Post a Comment