c#-System.DirectoryServices.DirectoryServicesCOMException:发生操作错误.
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-System.DirectoryServices.DirectoryServicesCOMException:发生操作错误.,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3847字,纯文字阅读大概需要6分钟。
内容图文
我正在尝试从在本地Intranet上提交ASP.NET表单的用户获取电子邮件地址.在我的本地计算机上测试时,它可以正常工作.但是,当我发布并开始在生产环境中对其进行测试时,它并不喜欢第74行.
Server Error in '/' Application.
--------------------------------------------------------------------------------
An operations error occurred.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.DirectoryServices.DirectoryServicesCOMException: An operations error occurred.
Source Error:
Line 71: adSearcher.SearchScope = SearchScope.Subtree;
Line 72: adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + userInQuestion + "))";
Line 73: SearchResult userObject = adSearcher.FindOne();
Line 74: if (userObject != null)
Line 75: {
Source File: c:\Web\Support-t\Content\Default.aspx.cs Line: 73
Stack Trace:
[DirectoryServicesCOMException (0x80072020): An operations error occurred.
]
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +628309
System.DirectoryServices.DirectoryEntry.Bind() +44
System.DirectoryServices.DirectoryEntry.get_AdsObject() +42
System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) +98
System.DirectoryServices.DirectorySearcher.FindOne() +44
_Default.Page_Load(Object sender, EventArgs e) in c:\Web\Support-t\Content\Default.aspx.cs:73
System.Web.UI.Control.LoadRecursive() +71
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18033
此代码块仅出于测试目的而在我的页面加载中,因此可以立即获得结果….但是它永远不会在生产环境中加载页面,但在本地计算机上的VS中调试时效果很好…
IIdentity id = WindowsIdentity.GetCurrent();
WindowsIdentity winId = id as WindowsIdentity;
if (id == null)
{
txtDetailedProblem.Text = "Identity is not a windows identity";
return;
}
string userInQuestion = winId.Name.Split('\\')[1];
string myDomain = winId.Name.Split('\\')[0]; // this is the domain that the user is in
// the account that this program runs in should be authenticated in there
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)
{
txtTEST.Text = prop.ToString() + " " + userObject.Properties[prop][0].ToString();
}
}
解决方法:
IIdentity id = WindowsIdentity.GetCurrent();
WindowsIdentity winId = id as WindowsIdentity;
if (id == null)
{
CurrentUserEmail = "identity is not a windows identity";
return;
}
var name = winId.Name;
string userInQuestion = name.Split('\\')[1];
string myDomain = name.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)
{ //when it works set variable to CurrentUserEmail instead of txtDetailPrblem textbox
CurrentUserEmail = userObject.Properties[prop][0].ToString();
}
}
}
内容总结
以上是互联网集市为您收集整理的c#-System.DirectoryServices.DirectoryServicesCOMException:发生操作错误.全部内容,希望文章能够帮你解决c#-System.DirectoryServices.DirectoryServicesCOMException:发生操作错误.所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。