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);
  • 相关阅读:
    Nginx如何配置Http、Https、WS、WSS?
    关于MySQL日志,我与阿里P9都聊了些啥?
    一文搞懂MySQL体系架构!!
    在业务高峰期拔掉服务器电源是一种怎样的体验?
    千万不要轻易尝试“熊猫烧香”,这不,我后悔了!
    从小白程序员到大厂高级技术专家我看过哪些书籍?
    聊一聊我在 B 站自学 Java 的经历吧
    计算机网络的 89 个核心概念
    MQTT 协议是个啥?这篇文章告诉你!
    1.5w字 + 24张图肝翻 TCP。
  • 原文地址:https://www.cnblogs.com/xh831213/p/1828211.html
Copyright © 2011-2022 走看看