根据多个用户角色填充下拉列表

时间:2018-10-16 19:15:23

标签: c# asp.net entity-framework webforms

我正在尝试根据角色填充DropDownList。我在SQL中有一个视图,其中包含基于用户角色(Windows Auth)的每个项目的值和文本,并且如果用户在所有角色中都具有以下内容,则可以填充DropDownList

 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();
            }

我想添加仅登录用户所属的项目。用户可以在多个组(角色)中。

例如,已登录的用户在一个名为Location 01 LOB 100的组中,并且他们也在一个Location 01 LOB 200Location o2 LOB 100的组中。仅那些选项应出现在DropDownList中。

我可以通过以下代码遍历用户所处的角色。

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);
                    }
                }

现在,我尝试结合2我遇到了问题,如果查询返回结果,则将loc.DataValue和loc.DataText添加到DDL。这是我遇到的问题,它将字符串加引号而不是值。

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);

        }
    }
}

谢谢

-道格

1 个答案:

答案 0 :(得分:0)

我通过创建用户所在的组列表然后使用适当的信息填充下拉列表来使其正常工作。

    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--");
        }
    }
相关问题