简体   繁体   中英

Populate Drop Down List based on multiple user roles

I am trying to populate a DropDownList based on roles. I have a view in SQL with the value and text of each item based on the user role (Windows Auth) and can populate the DropDownList if a user was in all roles with:

 using (var db=new GPSE_2.DAL.GPSE2Entities())
            {
                var locations = (from loc in db.LocationLOBViews
                                orderby loc.LocationNumber
                                select new { loc.DataValue, loc.DataText});
                ddlShops.DataValueField = "DataValue" ;
                ddlShops.DataTextField = "DataText";
                ddlShops.DataSource = locations.ToList();
                DataBind();
            }

I would like to add items only the logged in user is a member of. Users can be in multiple groups(roles).

For instance, the logged in user is in a group called Location 01 LOB 100 and they are also in a group called Location 01 LOB 200 and also in Location o2 LOB 100 . Only those options should appear in the DropDownList .

I was able to loop through the roles the user is in by the code below.

string UPN = UserPrincipal.Current.UserPrincipalName.ToString();
                WindowsIdentity wi = new WindowsIdentity(UPN);
                string GroupName;
                foreach (IdentityReference group in wi.Groups)
                {
                    GroupName = group.Translate(typeof(NTAccount)).ToString();
                    if (GroupName.Contains("Location 01 LOB 100"))
                    {
                        var item = new ListItem
                        {
                            Text = "Location 01 LOB 100",
                            Value = "01,100"
                        };
                        ddlShops.Items.Add(item);
                    }
                }

Now I am trying to combine the 2 I ran into problems adding loc.DataValue and the loc.DataText to the DDL if the query returns results. This is where I an stuck, it adds the string in the quotes instead of the values.

using (var db = new GPSE_2.DAL.GPSE2Entities())
{
    string UPN = UserPrincipal.Current.UserPrincipalName.ToString();
    WindowsIdentity wi = new WindowsIdentity(UPN);
    string GroupName;
    foreach (IdentityReference group in wi.Groups)
    {
        GroupName = group.Translate(typeof(NTAccount)).ToString();
        var locations = (from loc in db.LocationLOBViews
                         where loc.DataValue.Contains(GroupName)
                         orderby loc.LocationNumber
                         select new { loc.DataValue, loc.DataText });
        if (locations !=null)
        {
            var item = new ListItem
            {
                Text = "DataText",
                Value = "DataValue"
            };
            ddlShops.Items.Add(item);

        }
    }
}

Thanks,

-Doug

I got it working by creating a list of groups the user is in and then populating the Drop Down List with the appropriate information.

    private List<string> GetGroups()
    {
        string UPN = UserPrincipal.Current.UserPrincipalName.ToString();
        List<string> result = new List<string>();
        WindowsIdentity wi = new WindowsIdentity(UPN);
        foreach (IdentityReference group in wi.Groups)
        {

            string GroupName = group.Translate(typeof(NTAccount)).ToString();
            //if text location and lob is in the name add to results
            if (GroupName.Contains("Location") && GroupName.Contains("LOB"))
            {
                string DataValue1 = GroupName.Substring(GroupName.Length - 3);
                string DataValue2 = GroupName.Substring(GroupName.Length - 10, 2);
                string DataValue = DataValue2 + "," + DataValue1;
                result.Add(DataValue);
            }


        }
        result.Sort();
        return result;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            using (var db = new GPSE2Entities())
            {
                for (int i = 0; i < GetGroups().ToArray().Length; i++)
                {
                    string DataValue = GetGroups().ToArray()[i].ToString();
                    var locations = (from loc in db.LocationLOBViews
                                     where loc.DataValue == DataValue
                                     orderby loc.LocationNumber
                                     select loc).FirstOrDefault();
                    if (locations != null)
                    {
                        var item = new ListItem
                         {
                             Text = locations.DataText,
                             Value = locations.DataValue
                         };
                        ddlShops.Items.Add(item);
                    }

                }

            }
            ddlShops.Items.Insert(0, "--Select Location and LOB--");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM