2013-03-13 3 views
4

Следующий код работает только в том случае, если в IIS включена только проверка подлинности Windows для локальных пользователей в нашей сети.Получение UserPrincipal с аутентификацией Windows и анонимной аутентификацией на

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

В противном случае он выдает это исключение:

[ArgumentException: The (& (ObjectCategory = пользователь) (объектный = пользователь) (| (UserPrincipalName =) (DistinguishedName =) (имя =))) фильтр поиска недействительна.] System.DirectoryServices.ResultsEnumerator.MoveNext() +434305 System.DirectoryServices.SearchResultCollection.get_InnerList() +282 System.DirectoryServices.SearchResultCollection.get_Count() +9 System.DirectoryServices.AccountManagement. ADStoreCtx.FindPri ncipalByIdentRefHelper (тип principalType, String urnScheme, String urnValue, DateTime referenceDate, Boolean useSidHistory) +1898 System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef (тип principalType, String urnScheme, String urnValue, DateTime referenceDate) +85 System.DirectoryServices.AccountManagement .Principal.FindByIdentityWithTypeHelper (PrincipalContext контекст, тип principalType, Nullable`1 IdentityType, String identityValue, DateTime refDate) +211 System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity (PrincipalContext контекст, String 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 (отправитель объекта, EventArgs e) в 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 (Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3064

есть ли способ получить эту работу для получение наших локальных пользователей UserPrincipal , в то время как Windows и анонимная аутентификация включены?

ответ

0

Не знаете, как вы нашли 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.