找回密码
 注册

QQ登录

只需一步,快速开始

BCM 门户 IT世界 应用开发 查看内容

Active Directory via C#.Net 3.5

2012-2-6 10:04| 发布者: Test| 查看: 1841| 评论: 0|原作者: Raymund Macaalay|来自: Raymund Macaalay's Dev Blog

摘要: Before .Net managing Active Directory objects is a bit lengthy and you need a good knowledge on the principal store to have your head around on what you want to do. We ususally use the System.Director ...
#region Group Methods

/// \
/// Creates a new group in Active Directory
/// \
/// \The OU location you want to save your new Group\
/// \The name of the new group\
/// \The description of the new group\
/// \The scope of the new group\
/// \True is you want this group to be a security group, false if you want this as a distribution group\
/// \Retruns the GroupPrincipal object\
public GroupPrincipal CreateNewGroup(string sOU, string sGroupName, string sDescription, GroupScope oGroupScope, bool bSecurityGroup)
{
    PrincipalContext oPrincipalContext = GetPrincipalContext(sOU);

    GroupPrincipal oGroupPrincipal = new GroupPrincipal(oPrincipalContext, sGroupName);
    oGroupPrincipal.Description = sDescription;
    oGroupPrincipal.GroupScope = oGroupScope;
    oGroupPrincipal.IsSecurityGroup = bSecurityGroup;
    oGroupPrincipal.Save();

    return oGroupPrincipal;
}

/// \
/// Adds the user for a given group
/// \
/// \The user you want to add to a group\
/// \The group you want the user to be added in\
/// \Returns true if successful\
public bool AddUserToGroup(string sUserName, string sGroupName)
{
    try
    {
        UserPrincipal oUserPrincipal = GetUser(sUserName);
        GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);
        if (oUserPrincipal != null && oGroupPrincipal != null)
        {
            if (!IsUserGroupMember(sUserName, sGroupName))
            {
                oGroupPrincipal.Members.Add(oUserPrincipal);
                oGroupPrincipal.Save();
            }
        }
        return true;
    }
    catch
    {
        return false;
    }
}

/// \
/// Removes user from a given group
/// \
/// \The user you want to remove from a group\
/// \The group you want the user to be removed from\
/// \Returns true if successful
public bool RemoveUserFromGroup(string sUserName, string sGroupName)
{
    try
    {
        UserPrincipal oUserPrincipal = GetUser(sUserName);
        GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);
        if (oUserPrincipal != null && oGroupPrincipal != null)
        {
            if (IsUserGroupMember(sUserName, sGroupName))
            {
                oGroupPrincipal.Members.Remove(oUserPrincipal);
                oGroupPrincipal.Save();
            }
        }
        return true;
    }
    catch
    {
        return false;
    }
}

/// \
/// Checks if user is a member of a given group
/// \
/// \The user you want to validate\
/// \The group you want to check the membership of the user\
/// \Returns true if user is a group member\
public bool IsUserGroupMember(string sUserName, string sGroupName)
{
    UserPrincipal oUserPrincipal = GetUser(sUserName);
    GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);

    if (oUserPrincipal != null && oGroupPrincipal != null)
    {
        return oGroupPrincipal.Members.Contains(oUserPrincipal);
    }
    else
    {
        return false;
    }
}

/// \
/// Gets a list of the users group memberships
/// \
/// \The user you want to get the group memberships\
/// \Returns an arraylist of group memberships\
public ArrayList GetUserGroups(string sUserName)
{
    ArrayList myItems = new ArrayList();
    UserPrincipal oUserPrincipal = GetUser(sUserName);

    PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();

    foreach (Principal oResult in oPrincipalSearchResult)
    {
        myItems.Add(oResult.Name);
    }
    return myItems;
}

/// \
/// Gets a list of the users authorization groups
/// \
/// \The user you want to get authorization groups\
/// \Returns an arraylist of group authorization memberships\
public ArrayList GetUserAuthorizationGroups(string sUserName)
{
    ArrayList myItems = new ArrayList();
    UserPrincipal oUserPrincipal = GetUser(sUserName);

    PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetAuthorizationGroups();

    foreach (Principal oResult in oPrincipalSearchResult)
    {
        myItems.Add(oResult.Name);
    }
    return myItems;
}

#endregion

#region Helper Methods

/// \
/// Gets the base principal context
/// \
/// \Retruns the PrincipalContext object\
public PrincipalContext GetPrincipalContext()
{
    PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);
    return oPrincipalContext;
}

/// \
/// Gets the principal context on specified OU
/// \
/// \The OU you want your Principal Context to run on\
/// \Retruns the PrincipalContext object\
public PrincipalContext GetPrincipalContext(string sOU)
{
    PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);
    return oPrincipalContext;
}

#endregion

}

鲜花

握手

雷人

路过

鸡蛋

最新评论

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-6-13 05:59 , Processed in 0.017526 second(s), 15 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

返回顶部