生产时出现 Directory.Services 错误
以下代码在我本地计算机上的 Visual Studio 开发环境中运行良好。但是,当我将文件移动到 Windows 2008 R2 IIS 7.5 计算机时,出现以下错误:
[DirectoryServicesCOMException (0x80072020):操作错误 发生。 ] _Default.GetFullName(String strLoginName, String& STR_FIRST_NAME,字符串& STR_LAST_NAME,字符串& STR_DISPLAY_NAME, 字符串& STR_MAIL,字符串& STR_OFFICE_PHONE,字符串& STR_ADDRESS)中 c:\AuthTest\Default.aspx.cs:87 _Default.Page_Load(对象发送者, EventArgs e) 在 c:\AuthTest\Default.aspx.cs:23
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,对象 o、对象 t、EventArgs e) +25 System.Web.UI.Control.LoadRecursive() +71 System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint、布尔值 includeStagesAfterAsyncPoint) +3064
我在 IIS 上启用了 Windows 身份验证,因此我不确定是否缺少其他内容。我的本地计算机和网络服务器都在同一个域中。
这是我的代码:
using System;
using System.DirectoryServices;
using System.Web.Hosting;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Gets the extracted User Name using a method.
string strUserID = ExtractUserName(User.Identity.Name.ToString());
string STR_FIRST_NAME;
string STR_LAST_NAME;
string STR_DISPLAY_NAME;
string STR_MAIL;
string STR_OFFICE_PHONE;
string STR_ADDRESS;
GetFullName(strUserID, out STR_FIRST_NAME, out STR_LAST_NAME, out STR_DISPLAY_NAME,
out STR_MAIL, out STR_OFFICE_PHONE, out STR_ADDRESS);
lblHello.Text = "Your User ID is: " + strUserID;
TextBox1.Text =
"Your name is: " + STR_FIRST_NAME + " " + STR_LAST_NAME + Environment.NewLine +
"Display Name: " + STR_DISPLAY_NAME + Environment.NewLine +
"Email address: " + STR_MAIL + Environment.NewLine +
"Office Phone: " + STR_OFFICE_PHONE + Environment.NewLine +
"Address: " + STR_ADDRESS;
}
//Retrives User Name from DomainName\\UserName
private static string ExtractUserName(string path)
{
string[] userPath = path.Split(new char[] { '\\' });
return userPath[userPath.Length - 1];
}
public static string GetFullName(string strLoginName,
out string STR_FIRST_NAME,
out string STR_LAST_NAME,
out string STR_DISPLAY_NAME,
out string STR_MAIL,
out string STR_OFFICE_PHONE,
out string STR_ADDRESS)
{
string userName = ExtractUserName(strLoginName);
SearchResult result = null;
using (HostingEnvironment.Impersonate())
{
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(SAMAccountName={0})", userName);
search.PropertiesToLoad.Add("cn");
STR_FIRST_NAME = "";
STR_LAST_NAME = "";
STR_DISPLAY_NAME = "";
STR_MAIL = "";
STR_OFFICE_PHONE = "";
STR_ADDRESS = "";
try
{
result = search.FindOne();
foreach (System.Collections.DictionaryEntry direntry in result.Properties)
{
STR_FIRST_NAME = result.GetDirectoryEntry().Properties["givenName"].Value.ToString();
STR_LAST_NAME = result.GetDirectoryEntry().Properties["SN"].Value.ToString();
STR_DISPLAY_NAME = result.GetDirectoryEntry().Properties["DisplayName"].Value.ToString();
STR_MAIL = result.GetDirectoryEntry().Properties["mail"].Value.ToString();
STR_OFFICE_PHONE = result.GetDirectoryEntry().Properties["telephoneNumber"].Value.ToString();
STR_ADDRESS = result.GetDirectoryEntry().Properties["streetAddress"].Value.ToString();
}
return null;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
在 VS 测试环境中,我的本地计算机上一切正常。我可能缺少 IIS 中的某种配置?
提前致谢。
The following code works fine in Visual Studio Development enviroment on my local machine. However when I move the files to a Windows 2008 R2 IIS 7.5 machine I get the following error:
[DirectoryServicesCOMException (0x80072020): An operations error
occurred. ] _Default.GetFullName(String strLoginName, String&
STR_FIRST_NAME, String& STR_LAST_NAME, String& STR_DISPLAY_NAME,
String& STR_MAIL, String& STR_OFFICE_PHONE, String& STR_ADDRESS) in
c:\AuthTest\Default.aspx.cs:87 _Default.Page_Load(Object sender,
EventArgs e) in c:\AuthTest\Default.aspx.cs:23
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object
o, Object t, EventArgs e) +25 System.Web.UI.Control.LoadRecursive()
+71 System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+3064
I have Windows Authentication enabled on IIS so I'm not sure if I'm missing something else. Both my local machine and the web server are in the same domain.
Here is my code:
using System;
using System.DirectoryServices;
using System.Web.Hosting;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Gets the extracted User Name using a method.
string strUserID = ExtractUserName(User.Identity.Name.ToString());
string STR_FIRST_NAME;
string STR_LAST_NAME;
string STR_DISPLAY_NAME;
string STR_MAIL;
string STR_OFFICE_PHONE;
string STR_ADDRESS;
GetFullName(strUserID, out STR_FIRST_NAME, out STR_LAST_NAME, out STR_DISPLAY_NAME,
out STR_MAIL, out STR_OFFICE_PHONE, out STR_ADDRESS);
lblHello.Text = "Your User ID is: " + strUserID;
TextBox1.Text =
"Your name is: " + STR_FIRST_NAME + " " + STR_LAST_NAME + Environment.NewLine +
"Display Name: " + STR_DISPLAY_NAME + Environment.NewLine +
"Email address: " + STR_MAIL + Environment.NewLine +
"Office Phone: " + STR_OFFICE_PHONE + Environment.NewLine +
"Address: " + STR_ADDRESS;
}
//Retrives User Name from DomainName\\UserName
private static string ExtractUserName(string path)
{
string[] userPath = path.Split(new char[] { '\\' });
return userPath[userPath.Length - 1];
}
public static string GetFullName(string strLoginName,
out string STR_FIRST_NAME,
out string STR_LAST_NAME,
out string STR_DISPLAY_NAME,
out string STR_MAIL,
out string STR_OFFICE_PHONE,
out string STR_ADDRESS)
{
string userName = ExtractUserName(strLoginName);
SearchResult result = null;
using (HostingEnvironment.Impersonate())
{
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(SAMAccountName={0})", userName);
search.PropertiesToLoad.Add("cn");
STR_FIRST_NAME = "";
STR_LAST_NAME = "";
STR_DISPLAY_NAME = "";
STR_MAIL = "";
STR_OFFICE_PHONE = "";
STR_ADDRESS = "";
try
{
result = search.FindOne();
foreach (System.Collections.DictionaryEntry direntry in result.Properties)
{
STR_FIRST_NAME = result.GetDirectoryEntry().Properties["givenName"].Value.ToString();
STR_LAST_NAME = result.GetDirectoryEntry().Properties["SN"].Value.ToString();
STR_DISPLAY_NAME = result.GetDirectoryEntry().Properties["DisplayName"].Value.ToString();
STR_MAIL = result.GetDirectoryEntry().Properties["mail"].Value.ToString();
STR_OFFICE_PHONE = result.GetDirectoryEntry().Properties["telephoneNumber"].Value.ToString();
STR_ADDRESS = result.GetDirectoryEntry().Properties["streetAddress"].Value.ToString();
}
return null;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
Again everything works fine on my local machine in the VS Testing enviroment. I'm probably missing some kind of configuration in IIS?
Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先要检查 IIS 应用程序池标识是否具有对 AD 的正确权限。
另外,作为旁注,这里有一些关于您的 catch { throw ex}
http:// www.tkachenko.com/blog/archives/000352.html
First thing would be to check that the IIS application pool identity has the correct permissions to AD.
Also as a side note here is something to read regarding your catch { throw ex}
http://www.tkachenko.com/blog/archives/000352.html