zoukankan      html  css  js  c++  java
  • Silverlight DomainService ADHelper 查找用户信息


    namespace SBTOSNew.Web.ADDomainService
    {
        
    using System;
        
    using System.Collections.Generic;
        
    using System.ComponentModel;
        
    using System.ComponentModel.DataAnnotations;
        
    using System.Linq;
        
    using System.ServiceModel.DomainServices.Hosting;
        
    using System.ServiceModel.DomainServices.Server;
        
    using System.DirectoryServices;
        
    using System.Text;
        
    using System.Security.Principal;


        
    // TODO: Create methods containing your application logic.
        [EnableClientAccess()]
        
    public class ADHelper : DomainService
        {
            
    public string GetUserInfo(string ADPath, string ADUser, string ADPassword, string CurrentUserName)
            {

                DirectoryEntry objDirEnt 
    = GetUser(ADPath, ADUser, ADPassword, CurrentUserName);
                StringBuilder sbUserInfo 
    = new StringBuilder();
                
    if (objDirEnt != null)
                {
                    sbUserInfo.Append(
    "Name = " + objDirEnt.Name + Environment.NewLine);
                    sbUserInfo.Append(
    "Path = " + objDirEnt.Path + Environment.NewLine);
                    sbUserInfo.Append(
    "SchemaClassName = " + objDirEnt.SchemaClassName + Environment.NewLine);
                    sbUserInfo.AppendFormat(
    "\t{0} = ""memberOf");
                    sbUserInfo.Append(Environment.NewLine);
                    
    foreach (var objValue in objDirEnt.Properties["memberOf"])
                    {
                        sbUserInfo.AppendFormat(
    "\t\t{0}" + Environment.NewLine,GetGroupName(objValue.ToString()));
                    }
                }
                
    return sbUserInfo.ToString();
            }

            
    private DirectoryEntry GetUser(string ADPath, string ADUser, string ADPassword, string CurrentUserName)
            {
                DirectoryEntry de 
    = GetDirectoryObject(ADPath, ADUser, ADPassword);
                DirectorySearcher deSearch 
    = new DirectorySearcher();
                deSearch.SearchRoot 
    = de;
                deSearch.Filter 
    = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + CurrentUserName + "))";
                deSearch.SearchScope 
    = SearchScope.Subtree;
                SearchResult results 
    = deSearch.FindOne();
                
    if (results != null)
                {
                    de 
    = new DirectoryEntry(results.Path, ADUser, ADPassword, AuthenticationTypes.Secure);
                    
    return de;
                }
                
    else
                {
                    
    return null;
                }
            }

            
    private DirectoryEntry GetDirectoryObject(string ADPath, string ADUser, string ADPassword)
            {
                DirectoryEntry oDE;
                oDE 
    = new DirectoryEntry(ADPath, ADUser, ADPassword, AuthenticationTypes.Secure);
                
    return oDE;
            }

            
    private string GetGroupName(string objValue)
            {
                
    string groupName = "";
                
    if (objValue == null || objValue.Trim() == "")
                {
                    groupName 
    = "";
                }
                
    else
                {
                    
    string[] groupInfo = objValue.Split(new char[] { ',' });
                    
    foreach (string item in groupInfo)
                    {
                        
    if (item.StartsWith("CN="))
                        {
                            groupName 
    = item.Substring(3);
                        }
                    }
                }
                
    return groupName;
            }

            
    public string GetSystemUserInfo(string ADUser, string ADPassword)
            {
                GenericIdentity currentIdentity 
    = GetGenericIdentity();
                
    string identityName = currentIdentity.Name;
                
    string identityAuthenticationType = currentIdentity.AuthenticationType;
                
    string[] userinfo = identityName.Split(new char[] { '\\' });

                
    string ADPath = @"LDAP://" + userinfo[0];
                
    string CurrentUserName = userinfo[1];

                DirectoryEntry objDirEnt 
    = GetUser(ADPath, ADUser, ADPassword, CurrentUserName);
                StringBuilder sbUserInfo 
    = new StringBuilder();
                
    if (objDirEnt != null)
                {
                    sbUserInfo.Append(
    "Name = " + objDirEnt.Name + Environment.NewLine);
                    sbUserInfo.Append(
    "Path = " + objDirEnt.Path + Environment.NewLine);
                    sbUserInfo.Append(
    "SchemaClassName = " + objDirEnt.SchemaClassName + Environment.NewLine);
                    sbUserInfo.AppendFormat(
    "\t{0} = ""memberOf");
                    sbUserInfo.Append(Environment.NewLine);
                    
    foreach (var objValue in objDirEnt.Properties["memberOf"])
                    {
                        sbUserInfo.AppendFormat(
    "\t\t{0}" + Environment.NewLine, GetGroupName(objValue.ToString()));
                    }
                }
                
    return sbUserInfo.ToString();
            }

            
    private GenericIdentity GetGenericIdentity()
            {
                WindowsIdentity windowsIdentity 
    = WindowsIdentity.GetCurrent();
                
    string authenticationType = windowsIdentity.AuthenticationType;
                
    string userName = windowsIdentity.Name;
                GenericIdentity authenticatedGenericIdentity 
    =
                    
    new GenericIdentity(userName, authenticationType);

                
    return authenticatedGenericIdentity;
            }
        }
    }

    使用:

    string ADUser = txtUser.Text.Trim();
                
    string ADPassword = txtPW.Password.Trim();
                
    string ADPath = @"LDAP://" + txtDomain.Text.Trim();
                
    string CurrentUserName = txtCurrentUser.Text.Trim();
                InvokeOperation
    <string> getUserInfo = adHelper.GetUserInfo(ADPath, ADUser, ADPassword, CurrentUserName);
                getUserInfo.Completed 
    += new EventHandler(getUserInfo_Completed);
  • 相关阅读:
    这些哭笑不得的情景,每一个程序猿都可能面对
    创建并使用静态库(ar 命令)
    C++中typename和class的区别
    C++11 tuple
    c++ map与 qt QMap insert 区别
    各个函数消耗的时间profiling和内存泄漏valgrind
    Linux查看代码量
    Linux应用程序打包
    静态代码检查工具 cppcheck 的使用
    制作Linux下程序安装包——使用脚本打包bin、run等安装包
  • 原文地址:https://www.cnblogs.com/xh831213/p/1828211.html
Copyright © 2011-2022 走看看