2016-05-13 100 views
2

我有一个严重依赖AD身份验证的ASP.NET应用程序。我们最近爆发了一种服务,该服务过去与应用程序一起生活在它自己的解决方案中,以便我们能够单独维护它们,而不会影响依赖服务的用户,比如说,我们需要更改代码影响了应用程序的用户界面。检索AD组成员身份失败调试远程调用

虽然这给我带来了一个有趣的问题。当我调试时,我有我的本地应用程序副本指向服务的远程实例,该应用程序的环境副本也使用该实例。当我尝试使用本地应用程序验证用户的会员 - >调用远程服务时,出现以下异常:

System.Runtime.InteropServices.COMException(0x8007200A):该 指定的目录服务属性或值不不存在。

在System.DirectoryServices.DirectoryEntry.Bind(布尔 throwIfFail)在System.DirectoryServices.DirectoryEntry.Bind()
在System.DirectoryServices.DirectoryEntry.get_SchemaEntry()在 System.DirectoryServices.AccountManagement.ADStoreCtx.IsContainer (的DirectoryEntry 日)在 System.DirectoryServices.AccountManagement.ADStoreCtx..ctor(的DirectoryEntry ctxBase,布尔ownCtxBase,字符串username,字符串密码, ContextOptions选项)在 System.DirectoryServices.AccountManagement.PrincipalContext.CreateContextFromDirectoryEntry(的DirectoryEntry 进入)在 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.UserPrincipal.FindByIdentity(PrincipalContext 上下文,字符串identityValue)在 ******************。******************。**** **************。IsUserMemberOfGroup(String userName,String groupName)

当我尝试在同一组中验证同一用户,但使用Web浏览器来击中在遥控盒上的应用程序,然后点击远程服务,它作为一个蛤蜊​​感到高兴。

我使用的代码似乎非常简单。它必须与我的机器如何呼叫有关,但如果我可以解决问题,我会被贬低。

public static bool IsUserMemberOfGroup(string userName, string groupName) 
{ 
    try 
    { 
     PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "XXX"); 

     var user = GetUser(userName, ctx); 

     if (user == null) 
     { 
      Log4NetLogManager.LogError("Unable to find user " + userName); 
      return false; 
     } 

     // find the group in question 
     groupName = groupName.Replace("XXX\\", string.Empty); 
     GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupName); 

     if (group != null) return user.IsMemberOf(group); 

     Log4NetLogManager.LogError("Unable to find group " + groupName); 
     return false; 
    } 
    catch (Exception ex) 
    { 
     if (!ex.Message.Contains("Unknown error")) 
     { 
      Log4NetLogManager.LogException(string.Format("Error while checking if {0} is a member of {1}", userName, groupName), ex); 
     } 
     return false; 
    } 
} 

private static UserPrincipal GetUser(string userName, PrincipalContext ctx) 
{ 
    UserPrincipal user = null; 
    userName = userName.Replace("XXX\\", string.Empty); 

    try 
    { 
     user = UserPrincipal.FindByIdentity(ctx, userName); 
    } 
    catch 
    { 
    } 

    return user ?? UserPrincipal.FindByIdentity(ctx, userName); 
} 
+0

你是否在两种环境中检查你的.config文件?有什么区别? – JCM

+0

是 - 唯一的区别是环境特定的(即电子邮件主题,目标,记录位置等)。 – Marisa

+0

你检查了这个:http://stackoverflow.com/questions/29647024/the-specified-directory-service-attribute-or-value-does-not-exist? – JCM

回答

0

你很早就剥离了域名,所以你并没有真正尝试不同的第二次机会。

private static UserPrincipal GetUser(string userName, PrincipalContext ctx) 
{ 
    UserPrincipal user = null; 

    try 
    { 
     user = UserPrincipal.FindByIdentity(ctx, userName); 
    } 
    catch (Exception exc1) 
    { 
     Log4NetLogManager.LogError("First chance: " + exc1.Message); 
    } 
    userName = userName.Replace("XXX\\", string.Empty); 

    return user ?? UserPrincipal.FindByIdentity(ctx, userName); 
} 

事实上,我也注意到,有在堆栈跟踪一个DoDomainInit(无论是本地计算机可能是在不同的域或标识字符串参数可以表现不同的遥控盒)。请记住,如果您的计算机不属于该域,则必须指定user/pswd。

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "XXX", "ad_usr","ad_pswd"); 
var user = GetUser(userName, ctx); 

还要检查它是否可以通过改变你的本地计算机Application Pool Identity下,对网络服务运行,因为错误是类似于这样issue固定它。