C#实现AD域认证用户源代码

本文介绍了一个用于验证用户是否能在Active Directory (AD) 域中成功登录的方法。该方法通过检查用户提供的账户名和密码来确定其有效性,并能判断用户账户是否被启用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public bool IsPass(string userAccount, string password)

        {

 

                    string DomainName = “DC=SYQUEYRY,DC=COM,DC=CN”;

                    string ADPath = “LDAP://SYQUEYRY.COM.CN”;

                    string ADDomain = “SYQUEYRY”;

                    //获得当前域中的路径

                    string _ADPath = ADPath + "/" + ADDomain;

 

                    string domainAndUsername;

                    bool hasDomain = false;

                    if (userAccount.StartsWith(DomainName, StringComparison.CurrentCultureIgnoreCase))

                    {

                        hasDomain = true;

                    }

                    if (hasDomain)

                    {

                        domainAndUsername = userAccount;

                    }

                    else

                    {

                        domainAndUsername = DomainName + @"\" + userAccount;

                    }

                    DirectoryEntry entry = new DirectoryEntry(_ADPath, domainAndUsername, password);

                    DirectorySearcher search = new DirectorySearcher(entry);

                    if (hasDomain)

                    {

                        userAccount = userAccount.Substring(DomainName.Length + 1);

                    }

                    search.Filter = "(sAMAccountName=" + userAccount + ")";

                    search.PropertiesToLoad.Add("displayName");

                    SearchResult adUser = null;

                    try

                    { www.2cto.com

                        adUser = search.FindOne();

                        if (adUser == null)

                        {

                            _error = "域认证失败";

                        }

                        else

                        {

                            if (Convert.ToInt32(adUser.Properties["userAccountControl"][0]) == 2)

                            {

                                _myUser = new MyUser(userAccount, password, adUser.Properties["displayName"].ToString());

                            }

                            else

                            {

                                _error = "此用户已禁用";

                            }

                            adUser = null;

                        }

                    }

                    catch (Exception ex)

                    {

                        _error = ex.Message;

                        adUser = null;

                    }

                    finally

                    {

                        entry.Close();

                        entry = null;

                        search.Dispose();

                        search = null;

                    }

                    return adUser != null;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值