【发布时间】:2016-05-19 13:39:24
【问题描述】:
今天我有一个问题,我无法访问我的模型的值:
public class SpecificADModel
{
public string DisplayName { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string CompanyName { get; set; }
public string Department { get; set; }
public string JobTitle { get; set; }
public string AdressBusiness { get; set; }
public string DirectNumberBusiness { get; set; }
public string ReceptionNumberBusiness { get; set; }
}
我只想要这个模型的用户名。
public void SearchWholeUser(string fullName){
UserADSearch searchUser = new UserADSearch();
List<SpecificADModel> source = new List<SpecificADModel>();
source = searchUser.SearchFull(path, fullName);
//string username = the value of username comes here
//used to initialize the detailed info frame
initializeInfos(source);
GetChildRequests(username);
}
设置值工作正常(发生在另一个类中)。是否有可能从列表中获取价值? 我已经试过了:
string username = source[1].ToString();
但它也不起作用。
知道如何在不发出新的广告请求的情况下获取用户名吗?
提前致谢
编辑
SearchFull() 喜欢这样(但我认为这没有必要):
public List<SpecificADModel> SearchFull(string direction, string fullName)
{
//New List in the SpecificADModel format.
List<SpecificADModel> users = new List<SpecificADModel>();
try
{
// New AD search for clicked user on the lefthandside ListView
DirectoryEntry entry = new DirectoryEntry(direction, "admin", "pass", AuthenticationTypes.Secure);
//searches for all relevant Infos in AD
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(&(objectCategory=person)(objectClass=user)(&(cn=" + fullName + ")))";
searcher.PropertiesToLoad.Clear();
searcher.PropertiesToLoad.Add("displayName");
searcher.PropertiesToLoad.Add("samAccountName");
searcher.PropertiesToLoad.Add("givenname");
searcher.PropertiesToLoad.Add("sn");
searcher.PropertiesToLoad.Add("company");
searcher.PropertiesToLoad.Add("department");
searcher.PropertiesToLoad.Add("title");
searcher.PropertiesToLoad.Add("streetAddress");
searcher.PropertiesToLoad.Add("homephone");
searcher.PropertiesToLoad.Add("telephoneNumber");
SearchResult result = searcher.FindOne();
string displayName = result.Properties["displayName"][0].ToString();
string accountName = result.Properties["samAccountName"][0].ToString();
string firstName = result.Properties["givenname"][0].ToString();
string lastName = result.Properties["sn"][0].ToString();
string companyName = result.Properties["company"][0].ToString();
string departmentName = result.Properties["department"][0].ToString();
string jobTitle = result.Properties["title"][0].ToString();
string streetAddress = result.Properties["streetAddress"][0].ToString();
string homePhone = result.Properties["homephone"][0].ToString();
string directPhone = result.Properties["telephoneNumber"][0].ToString();
//Results will be stored in SpeceficADModel Format in the users ListView
users.Add(new SpecificADModel()
{
DisplayName = displayName,
Username = accountName,
FirstName = firstName,
LastName = lastName,
CompanyName = companyName,
Department = departmentName,
JobTitle = jobTitle,
AdressBusiness = streetAddress,
ReceptionNumberBusiness = homePhone,
DirectNumberBusiness = directPhone
});
//returns List<>
return users;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
Console.WriteLine(e.ToString());
return null;
}
}
【问题讨论】:
-
我不太明白这个问题,但我认为这取决于 SearchFull 在里面做了什么。
-
也许
string username = source[1].Username;? -
您是否在添加完属性后重新构建了项目? :)
标签: c# .net properties model