2013-03-13 3 views
4

다음 코드는 우리 네트워크의 로컬 사용자에 대해 IIS에서 Windows 인증 만 사용하는 동안에 만 작동합니다.Windows 인증 및 익명 인증을 사용하여 UserPrincipal을 가져 오는 중

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
{ 
    UserPrincipal up = UserPrincipal.FindByIdentity(ctx, userName); 
    return up; 
} 

그렇지 않으면이 예외가 발생 :

[ArgumentException이 다음 (& (수 objectcategory = 사용자() 방법 objectClass = 사용자() | (userPrincipalName 사용 =()에 distinguishedName =) (NAME =))) 검색 필터가 유효하다.] System.DirectoryServices.ResultsEnumerator.MoveNext() 434,305 System.DirectoryServices.SearchResultCollection.get_InnerList() 282 System.DirectoryServices.SearchResultCollection.get_Count() +9 System.DirectoryServices.AccountManagement. ADStoreCtx.FindPri ncipalByIdentRefHelper (유형 principalType, 문자열 urnScheme, 문자열 urnValue, 날짜 시간 referenceDate, 부울 useSidHistory) 1898 System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef (유형 principalType, 문자열 urnScheme, 문자열 urnValue, 날짜 시간 referenceDate) 85 System.DirectoryServices.AccountManagement .Principal.FindByIdentityWithTypeHelper (PrincipalContext 상황, 유형 principalType, Nullable`1 identityType, 문자열 identityValue, 날짜 시간 refDate) 211 System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity (PrincipalContext 컨텍스트, 문자열 identityValue) 95 WebApplication1.Index.GetUserPrincipal (String userName) C : \ Users \ xxx \ Documents \ Visual Studio 2010 \ Projects \ WebApplication1 \ WebApplication1 \ Index.aspx.cs : 38 WebApplication1.Index.Page_Load (Object sender, EventArgs e) in C : \ Users \ xxx \ Documents \ Visual Studio 2010 \ Projects \ WebApplication1 \ WebApplication1 \ Index.aspx.cs : 19 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 (부울 includeStagesBeforeAsyncPoint, 부울 includeStagesAfterAsyncPoint)

3064은 작동이를 얻는 방법이 있나요 로컬 사용자 UserPrincipal 을 얻는 동안 Windows 및 익명 인증은 모두으로 설정되어 있습니까?

답변

0

ID 유형을 지정하는 데 필자는 FindByIdentity가 작동하는 방법을 잘 모르고 있습니까? 예 :

UserPrincipal up = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userName); 

어떤 방법으로도 가장을 강요하면됩니다. 그 코드는 다음 사용 따라서 전에 :

// This will impersonate the logged in user in order to get whichever username you require GIVEN the logged in user has AD read/querying rights. 

System.Web.HttpContext.Current.Request.LogonUserIdentity.Impersonate(); 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
    { 
    UserPrincipal up = UserPrincipal.FindByIdentity(ctx, userName); 
    return up; 
    } 
1

userName은 빈 문자열 (또는 다른 방법으로, 완전히 공백으로 구성), 그리고 분명히 그것은 FindByIdentity에 의해 검증 아니에요해야합니다.