惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
Google Developers Blog
J
Java Code Geeks
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
人人都是产品经理
人人都是产品经理
Martin Fowler
Martin Fowler
IT之家
IT之家
博客园_首页
B
Blog RSS Feed
Google DeepMind News
Google DeepMind News
B
Blog
U
Unit 42
Apple Machine Learning Research
Apple Machine Learning Research
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog
博客园 - 聂微东
腾讯CDC
A
About on SuperTechFans

博客园 - pot

在框架中(IFRAME/FRAMESET)传递COOKIE的解决方案[转] jQuery图片播放插件Fancybox使用方法 C#生成缩略图代码 数据抓取的一个类,包含一些常用的方法 抓取AJAX网页的方法-Firefox组件,C#集成 C#编号的ActiveX控件采用CAB的布署方式实例 C#编写ActiveX控件实例(包括命令和事件) PdfAcroViewer 用XML反序列化快速完成ASP.NET配置文件 - pot - 博客园 android Activity类的使用 Android中的Intent详细讲解 Android模拟器常用使用,和基本功能使用 ZPL II 命令参考 正则表达式语法 - pot - 博客园 《C#异常处理》 C#中接口的作用 The RSA key container could not be opened - pot ASP.NET 页面事件执行顺序 关于object sender,EventArgs e 的一些解释
C#代码登录活动目录
pot · 2010-07-23 · via 博客园 - pot

代码

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;namespace ChangeControlApp
{
    
class LdapAuthentication
    {
        
private String _path;
        
private String _filterAttribute;public LdapAuthentication(String path)
        {
            _path 
= path;
        }
public bool IsAuthenticated(String domain, String username, String pwd)
        {
            String domainAndUsername 
= domain + @"\" + username;
            DirectoryEntry entry 
= new DirectoryEntry(_path, domainAndUsername, pwd);try
            {    
//Bind to the native AdsObject to force authentication.            
                Object obj = entry.NativeObject;

                DirectorySearcher search 

= new DirectorySearcher(entry);

                search.Filter 

= "(SAMAccountName=" + username + ")";
                search.PropertiesToLoad.Add(
"cn");
                SearchResult result 
= search.FindOne();if (null == result)
                {
                    
return false;
                }
//Update the new path to the user in the directory.
                _path = result.Path;
                _filterAttribute 
= (String)result.Properties["cn"][0];
            }
            
catch (Exception ex)
            {
                
throw new Exception("Error authenticating user. " + ex.Message);
            }
return true;
        }
public String GetGroups()
        {
            DirectorySearcher search 
= new DirectorySearcher(_path);
            search.Filter 
= "(cn=" + _filterAttribute + ")";
            search.PropertiesToLoad.Add(
"memberOf");
            StringBuilder groupNames 
= new StringBuilder();try
            {
                SearchResult result 
= search.FindOne();int propertyCount = result.Properties["memberOf"].Count;

                String dn;

int equalsIndex, commaIndex;for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
                {
                    dn 
= (String)result.Properties["memberOf"][propertyCounter];

                    equalsIndex 

= dn.IndexOf("="1);
                    commaIndex 
= dn.IndexOf(","1);
                    
if (-1 == equalsIndex)
                    {
                        
return null;
                    }

                    groupNames.Append(dn.Substring((equalsIndex 

+ 1), (commaIndex - equalsIndex) - 1));
                    groupNames.Append(
"|");

                }
            }

catch (Exception ex)
            {
                
throw new Exception("Error obtaining group names. " + ex.Message);
            }
            
return groupNames.ToString();
        }

    }
}

ad path 传值类似:LDAP://Asia2.ccb2.com