Home > .net 3.5, c#, Security, Visual Studio 2008 > Getting at the Membership Roles.GetAllRoles()

Getting at the Membership Roles.GetAllRoles()

February 13, 2008

For the WebGallery2 project, I’ve moved away from my own user management and opted for the .NET Roles and Membership “features”.  I do like the functionality (built in is good sometimes), and the ease of maintenance; however, the management of roles and memberships SUCKS—especially when you want to use that information in your application.

One such is our Galleries.  Each gallery has an associated Role that is checked by the User.IsInRole() method.

However, when editing a gallery, I want to see the enumeration for all roles—so I can pick and choose in a drop down list.  Unfortunately, there’s no really good way to get at the Roles list and drop it into a GridView’s FooterRow—at least not that seems to work without 40–50 lines of code.

So, I ended up cheating a bit.

Inside our LINQ DataContext, I added a non-LINQ-releated method called “GetAllRoles()” that returns a string array (string[]).  To me, this is still data, data retrieval, and fits into the DataContext partial class.

public string[] GetAllRoles()


return Roles.GetAllRoles();


I can now call that method like:

WebGalleryDataContext db = new WebGalleryDataContext();

string[] rolesList = db.GetAllRoles();

That’s great.  However, if I wanted to use a LinqDataSource adapter, that won’t work—it doesn’t exist inside a LINQ Table context.  To fix that, the good old ObjectDataAdapter comes to the rescue.

<asp:ObjectDataSource ID=”Roles” runat=”server”


TypeName=”WebGallery.Models.WebGalleryDataContext” />

Then, in my EditItemTemplate (of a GridView), I can populate the DropDownList with the roles in the system:

<asp:DropDownList runat=”server” ID=”EditRole”
   SelectedValue=’<%# Bind(“Role”) %> />

ObjectDataSource at work

If there’s a better way, I’d LOVE to hear it.🙂

Update: Whoops, http://localhost for the photo probably isn’t gonna work.

  1. anthony
    April 18, 2008 at 12:11 pm

    Interesting post, thank you for sharing.

    Did you have any trouble initially accessing this new feature of the framework?

    I’m a developer but not an administrator and was wondering if that is why
    Roles.GetAllRoles() returns nothing, ditto for Membership.GetAllUsers() for me on my local IIS at work. is this a rights issue? I get no error.

    Can’t seem to find an answer in Microsoft site (or google).


    string[] rolesArray;
    MembershipUserCollection users;
    string[] usersInRole;

    public void Page_Load()
    Msg.Text = “”;

    if (!IsPostBack)
    // Bind roles to ListBox.
    try {
    rolesArray = Roles.GetAllRoles();
    Response.Write(“Roles ” + rolesArray.Length + “” + Page.User.IsInRole(“Administrators”).ToString() + “” + Page.User.IsInRole(“Information Resources Services WA”).ToString()+””);
    RolesListBox.DataSource = rolesArray;
    catch (Exception ex)
    Response.Write(“colud not view roles” + Server.HtmlEncode(ex.Message));

    Response.Write(“TEST ” + Page.User.Identity.Name);
    // Bind users to ListBox.

    // Try
    // Roles.DeleteRole(lbxAvailableRoles.SelectedValue)

    // lblResults.Text = Nothing
    // lblResults.Visible = False
    //Catch ex As Exception
    // lblResults.Text = “Could not delete the role: ” + Server.HtmlEncode(ex.Message)
    // lblResults.Visible = True
    //End Try
    users = Membership.GetAllUsers();
    UsersListBox.DataSource = users;
    catch (Exception ex)
    Response.Write(“colud not view users” + Server.HtmlEncode(ex.Message));

    if (RolesListBox.SelectedItem != null)
    // Show users in role. Bind user list to GridView.

    usersInRole = Roles.GetUsersInRole(RolesListBox.SelectedItem.Value);
    UsersInRoleGrid.DataSource = usersInRole;

  2. April 20, 2008 at 5:27 pm


    Do you have Roles authentication turned on and are you logged in when running this code? If you debug your code and step through it, try creating an instance of User and see what’s returned.

  3. soniya
    March 13, 2009 at 11:03 am

    While creating a role i need to know i need to associate one more user id.
    How do i do it.
    (i am using VB.NET & VS2008)

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: