允许匿名用户访问的AD验证方法(1)

using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.DirectoryServices;
using System.Web.Configuration;
using System.Collections;


/// <summary>
/// ADOprate 的摘要说明
/// </summary>
public class ADOprate
{
    private string _ADPath; //域路径
    private string _Domain; //域名称
    private string _userID;//要模拟的用户名称
    private string _Pass;    //用户密码
    public IdentityImpersonation _identityimpersonation;//包含程序模拟方法的类实例

    //默认构造函数,初始化,并读取配置文件指定信息
    public ADOprate(string userID, string Pass)
    {
        this._ADPath = System.Configuration.ConfigurationManager.AppSettings["LDAPString"];
        this._Domain = "GreatWall";
        this._userID = userID;
        this._Pass = Pass;
        this._identityimpersonation = new IdentityImpersonation(this._userID,this._Pass,this._Domain);

        //获取配置文件信息
        //Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
        //SystemWebSectionGroup ws = (SystemWebSectionGroup)config.GetSectionGroup("system.web");
        //IdentitySection iden = ws.Identity;
        //this._userID = iden.UserName;
        //this._Pass = iden.Password;
    }


    //创建要模拟运行的用户,以获取权限
    private DirectoryEntry GetDirectoryObject()
    {
        DirectoryEntry entry = new DirectoryEntry(_ADPath, this._userID, this._Pass, AuthenticationTypes.Secure);
        return entry;
    }


    //创建属于用户自己的身份,并具有默认权限
    private DirectoryEntry GetDirectoryObject(string userID, string UserPass)
    {
        DirectoryEntry entry = new DirectoryEntry(_ADPath, userID, UserPass, AuthenticationTypes.Secure);
        return entry;
    }


    //根据指定用户名和密码判断是否合法用户,并返回用户信息;
    public UserEntity IsValidateUser(string userID, string pwd)
    {
        if (this._identityimpersonation.LookUpAccount(userID))
        {
            if (this._identityimpersonation.BeginImpersonate())
            {
                string domainAnduserID = this._Domain + @"\" + userID;
                DirectoryEntry entry = new DirectoryEntry(_ADPath, domainAnduserID, pwd);
                try
                {
                    Object obj = entry.NativeObject;
                }
                catch
                {
                    return null;
                }
                this._userID = userID;
                this._Pass = pwd;
                UserEntity user = GetUserMsg(userID);
                this._identityimpersonation.StopImpersonate();
                return user;
            }
        }
        return null;
       
    }

    private bool IsManager(DirectoryEntry entry, string userID)
    {

        PropertyValueCollection groups = entry.Properties["memberOf"];
        foreach (object PropertyValue in groups)
        {
            if (PropertyValue.ToString().IndexOf("OU") != -1)
                return true;
        }
        return false;
    }


    //返回指定用户的节点对象
    public UserEntity GetUserMsg(string userID)
    {
        if (this._identityimpersonation.LookUpAccount(userID))
        {
            if (this._identityimpersonation.BeginImpersonate())
            {
                DirectorySearcher search = new DirectorySearcher(GetDirectoryObject());
                search.Filter = "(SAMAccountName=" + userID + ")";
                //search.PropertiesToLoad.Add("CN");
                SearchResult result = search.FindOne();
                if (null == result)
                {
                    this._identityimpersonation.StopImpersonate();
                    return null;
                }
                else
                {
                    DirectoryEntry entry = new DirectoryEntry(result.Path);
                    UserEntity userEntity = new UserEntity();
                    userEntity.UserID = userID;
                    userEntity.DePartName = entry.Properties["description"][0].ToString();
                    userEntity.UserName = entry.Properties["CN"][0].ToString();

                    //设置其权限
                    if (IsManager(entry, userID))
                        userEntity.PositionLevel = "1";
                    else userEntity.PositionLevel = "-1";
                    this._identityimpersonation.StopImpersonate();
                    return userEntity;
                }
            }
           
        }
        return null;
    }

    //更具组名返回属于该组的所有成员信息
    private SearchResultCollection GetUsersByGroup(string GroupName)
    {
        if (this._identityimpersonation.BeginImpersonate())
        {
            DirectorySearcher search = new DirectorySearcher(GetDirectoryObject());
            search.Filter = "(description=" + GroupName + ")";
            search.SearchScope = SearchScope.Subtree;
            SearchResultCollection results = search.FindAll();
            this._identityimpersonation.StopImpersonate();
            return results;
        }
        return null;
    }
   
    //以二维数组的形式返回指定组下的所有成员信息
    public string[,] GetUserMsgByGroup(string GroupName)
    {
        string[,] UsermsgArray = null;

        System.Web.Caching.Cache cache = HttpRuntime.Cache;
        if (cache[GroupName] != null)
        {
            // Create DataTable From Cache
            UsermsgArray = (string[,])cache[GroupName];
        }
        else
        if (this._identityimpersonation.BeginImpersonate())
        {
            SearchResultCollection results = GetUsersByGroup(GroupName);
            int count = results.Count;
            UsermsgArray = new string[count, 2];
            int i = 0;
            foreach (System.DirectoryServices.SearchResult result in results)
            {
                DirectoryEntry de = new DirectoryEntry(result.Path);
                UsermsgArray[i, 0] = de.Properties["sAMAccountName"][0].ToString();
                UsermsgArray[i, 1] = de.Properties["CN"][0].ToString();
                i++;
            }
            this._identityimpersonation.StopImpersonate();

            TimeSpan SessTimeOut = new TimeSpan(0,0,System.Web.HttpContext.Current.Session.Timeout,0,0);
            cache.Insert(GroupName, UsermsgArray, null, DateTime.MaxValue, SessTimeOut,System.Web.Caching.CacheItemPriority.NotRemovable,null);
        }

    return UsermsgArray;
    }

}

posted on 2006-11-25 16:30 空谷幽兰 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/wmhysu/archive/2006/11/25/572191.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值