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

 

 

转载于:https://www.cnblogs.com/xh831213/archive/2010/09/16/1828211.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值