确定是否用户在AD组的.NET 4.0应用程序 [英] Determine if user is in AD group for .NET 4.0 application

查看:175
本文介绍了确定是否用户在AD组的.NET 4.0应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,以确定如果用户是Active Directory(AD)组内部的ASP.NET 4.0应用程序中的一员。下面的code抛出一个试图访问已卸载的应用程序域的时候,用户不是AD组的成员异常错误在最后一行(return语句)的情况下。

I am trying to determine if a user is a member of an Active Directory (AD) group for an internal ASP.NET 4.0 application. The code below throws an "Attempted to access an unloaded appdomain" exception error on the last line (return statement) in the case when the user is not a member of the AD group.

public static bool IsInADGroup(string userName, string groupName)
{
    var principalContext = new PrincipalContext(ContextType.Domain);
    UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, userName);
    if (userPrincipal == null)
        return false;

    GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName);
    if (groupPrincipal == null)
        return false;

      return userPrincipal.IsMemberOf(groupPrincipal);
}

就如何解决或其他解决方法任何想法?

Any ideas on how to fix or other workarounds?

推荐答案

莫非<一href="http://connect.microsoft.com/VisualStudio/feedback/details/605939/principalcontext-groupprincipal-unknown-error">this错误是你的问题?

我已经解决了使用此解决办法同样的问题:

I've solved the same problems using this workaround:

           using (DirectoryEntry rootDse = new DirectoryEntry("LDAP://rootdse"))
        {
            var dnsName = rootDse.Properties["dnsHostName"].Value.ToString();
            using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dnsName)) {}

这篇关于确定是否用户在AD组的.NET 4.0应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆