Here's the problem. The dropdown list in my view in an application I'm working on is showing SQLServer2005SQLBrowserUser$DONSERVER instead of the actual Active Directory group name. I'm posting here for the first time. Personally I have never ran into this issue. I'm hoping someone with experience with this can help out. I have posted the relevant code below.
Code to get all Active Directory groups (note: the code is located in another model):
public List<GroupsModel> PopulateGroups(GroupsModel groupsModel)
{
List<GroupsModel> GetGroups = new List<GroupsModel>();
// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// define a "query-by-example" principal - here, we search for a GroupPrincipal
GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
// find all matches
foreach (var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
groupsModel.SamAccountName = found.SamAccountName;
groupsModel.Description = found.Description;
groupsModel.DistinguishedName = found.DistinguishedName;
GetGroups.Add(groupsModel);
}
return GetGroups;
}
Here is the markup associated with retrieving the Active Directory groups - note: at the top of the page I have...
@using Microsoft.AspNet.Identity;,
@model Active_Directory_Manager.Models.MainPageModel
@using System.Web.UI.WebControls;
At the modal section of my view I'm using this chunk of code.
@{
Active_Directory_Manager.Models.GroupsLists groupLists = new Active_Directory_Manager.Models.GroupsLists();
Active_Directory_Manager.Models.GroupsModel groupsModel = new Active_Directory_Manager.Models.GroupsModel();
}
@foreach (var item in groupLists.PopulateGroups(groupsModel))
{
<select class="input-lg" style="">
<option value="0" style="color:#ccc !important;">
Please select a group to join to...
</option>
<option value="@item.SamAccountName">@item.SamAccountName</option>
</select>
}
Thank you in advance if anyone can help me out. In the meantime I'll do my best to solve this problem.
I have tried several things to help solve my problem. I tried using the private List<GroupsModel> outside of the public List<GroupsModel> PopulateGroups method and also tried using it inside of that method as well.
I also tried to access the list via the @Model.ModelName (for example) but the model kept coming up null while in debug mode.
I actually ended up solving this issue myself. The problem was I had
List<GroupModel>instead ofList<string>. I changed it toList<string>and got all Active Directory Groups. Thanks :D