所以我遇到了这个问题,有人试图加载这个没有电子邮件地址的页面,我很难找到解决办法。
//getting email address for current user
IIdentity id = WindowsIdentity.GetCurrent();
WindowsIdentity winId = id as WindowsIdentity;
if (id != null)
{
userName = winId.Name;
string userInQuestion = userName.Split('\\')[1];
string myDomain = userName.Split('\\')[0]; // this is the domain that the user is in
// the account that this program runs in should be authenticated in there
using (HostingEnvironment.Impersonate())
{
DirectoryEntry entry = new DirectoryEntry("LDAP://" + myDomain);
DirectorySearcher adSearcher = new DirectorySearcher(entry);
adSearcher.SearchScope = SearchScope.Subtree;
adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + userInQuestion + "))";
SearchResult userObject = adSearcher.FindOne();
if (userObject != null)
{
string[] props = new string[] { "mail"};
{
foreach (string prop in props)
{
if (userObject.Properties[prop][0].ToString() != "")
{
CurrentUserEmail = userObject.Properties[prop][0].ToString();
}
else
{
CurrentUserEmail = "";
}
}
}
}
}
}
这是我得到的堆栈跟踪,我知道有些提交此表单的人可能没有电子邮件地址,但我似乎无法找到解决该问题的方法。那里的 if else 语句是我最近的尝试,但错误仍然存在。