asp.net应用程序userprincipal.findbyidentity服务器与浏览器的工作原理,从我的机器抛出异常 [英] asp.net application userprincipal.findbyidentity works with browser on server, throws exception from my machine
问题描述
我有一个在IIS 7的服务器上运行,在这个节目,我需要找到所有当前用户是其成员组的应用程序。当我使用浏览器访问该网站的服务器上,它完美的作品,但是当我试图从我的机器访问它,它不断抛出COM异常,这里是code我用得到的用户群体。
私人列表<串GT; GetUserGroups(用户名字符串)
{
//对于输出字符串的列表。
清单<串GT;输出=新的List<串GT;();
尝试
{
//在使用块,便于处置创建PrincipalContext对象
使用(PrincipalContext域=新PrincipalContext(ContextType.Domain,域))
//使用(用户的WindowsIdentity = WindowsIdentity.GetCurrent())
{ //通过查找用户创建从一个PrincipalContext的UserPrincipal
//传递给函数这是一个不断抛出异常的行。
使用(UserPrincipal用户= UserPrincipal.FindByIdentity(域,IdentityType.SamAccountName,用户名))
{
//检查以确保用户被发现。
如果(用户!= NULL)
{
//获取用户组的集合变量称为组
PrincipalSearchResult<主>基团= UserPrincipal.Current.GetAuthorizationGroups();
// IdentityReferenceCollection组= user.Groups;
//这个foreach循环经过每个结果的收集组
的foreach(在组首席P)
{
//检查结果是GroupPrincipal对象,不为null
如果(p是GroupPrincipal&放大器;&安培; p.ToString()!= NULL)
{
output.Add(p.ToString()); //将字符串值添加到输出列表。
debugString + =< BR />中+ p.ToString();
}
}
}
}
}
}
赶上(异常前)
{
processLog.Text + = ex.ToString()+ ex.GetType();
}
//返回组的用户是其成员的列表。
返回输出;
}
为什么会抛出异常,当我从服务器以外的地方访问它?我怎样才能解决这个问题?
更新:
这里是堆栈跟踪例外,所有的
System.Runtime.InteropServices.COMException(0x80072020):一个
发生操作错误。在
System.DirectoryServices.DirectoryEntry.Bind(布尔throwIfFail)在
System.DirectoryServices.DirectoryEntry.Bind()在
System.DirectoryServices.DirectoryEntry.get_AdsObject()在
System.DirectoryServices.PropertyValueCollection.PopulateList()在
System.DirectoryServices.PropertyValueCollection..ctor(的DirectoryEntry
进入,字符串propertyName的)在
System.DirectoryServices.PropertyCollection.get_Item(字符串
propertyName的)在
System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()
在
System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
在
System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
在
System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
在
System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext
背景,类型principalType,Nullable`1 identityType,字符串
identityValue,日期时间refDate)在
System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext
背景,类型principalType,IdentityType identityType,字符串
identityValue)在
System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext
背景下,IdentityType identityType,字符串identityValue)在
ResetUnlockAccount.ResetUnlockAccount.GetUserGroups(用户名字符串)
在
C:\\ ResetUnlockAccount \\ ResetUnlockAccount \\ ResetUnlockAccount.aspx.cs:行
894
块引用>解决方案%的OP的评论,
答案在这里找到: GroupPrincipal方法FindByIdentity抛出异常诡异
只是不得不添加
使用System.Web.Hosting;
和
使用(HostingEnvironment.Impersonate())
在首先使用在
原来的code。
块引用>I have an application that is running on an IIS 7 server, in this program I need to find all the groups that the current user is a member of. When I access the website using the browser on the server, it works perfectly, but when I try to access it from my machine it keeps throwing a COM exception, Here is the code I'm using to get the user groups.
private List<string> GetUserGroups(string userName) { //The list of strings for output. List<string> output= new List<string>(); try { //creating a PrincipalContext object in a using block for easy disposal using(PrincipalContext domain = new PrincipalContext(ContextType.Domain,"domain")) //using(WindowsIdentity user = WindowsIdentity.GetCurrent()) { //Creating a UserPrincipal from the PrincipalContext by finding the user that //was passed to the function This is the line that keeps throwing the exception. using (UserPrincipal user = UserPrincipal.FindByIdentity(domain,IdentityType.SamAccountName,userName)) { //Checking to make sure the user was found. if (user != null) { //Getting the users groups in a collection variable called groups PrincipalSearchResult<Principal> groups = UserPrincipal.Current.GetAuthorizationGroups(); //IdentityReferenceCollection groups = user.Groups; //This foreach loop goes through each result in the groups collection foreach (Principal p in groups) { //check the result is a GroupPrincipal object and is not null if (p is GroupPrincipal && p.ToString() != null) { output.Add(p.ToString());//Add the string value to the output list. debugString += "<br/>"+p.ToString(); } } } } } } catch (Exception ex) { processLog.Text += ex.ToString()+ ex.GetType(); } //return the list of groups the user is a member of. return output; }
Why does it throw the exception when I access it from a location other than the server? How can I fix it?
Update: Here is the stacktrace exception and all
System.Runtime.InteropServices.COMException (0x80072020): An operations error occurred. at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) at System.DirectoryServices.DirectoryEntry.Bind() at System.DirectoryServices.DirectoryEntry.get_AdsObject() at System.DirectoryServices.PropertyValueCollection.PopulateList() at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) at System.DirectoryServices.PropertyCollection.get_Item(String propertyName) at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, IdentityType identityType, String identityValue) at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue) at ResetUnlockAccount.ResetUnlockAccount.GetUserGroups(String userName) in C:\ResetUnlockAccount\ResetUnlockAccount\ResetUnlockAccount.aspx.cs:line 894
解决方案Per the OP's comment,
The answer was found here: GroupPrincipal method FindByIdentity throw strange exception
Just had to add
using System.Web.Hosting;
andusing(HostingEnvironment.Impersonate())
over the first using in the original code.
这篇关于asp.net应用程序userprincipal.findbyidentity服务器与浏览器的工作原理,从我的机器抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!