gpt4 book ai didi

c# - System.DirectoryServices.DirectoryServicesCOMException : An operations error occurred.

转载 作者:行者123 更新时间:2023-11-30 16:15:37 36 4
gpt4 key购买 nike

我正在尝试获取在本地 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 : An operations error occurred.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19324779/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com