Silverlight -DomainService ADHelper 查詢使用者資訊

weixin_34402090發表於2010-09-16

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);

 

 

相關文章