1. 程式人生 > 實用技巧 >C# AD域驗證登入

C# AD域驗證登入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var userName = "abc";
            
var password = "123456"; using (DirectoryEntry adsEntry = new DirectoryEntry("LDAP://192.168.0.109", userName, password, AuthenticationTypes.Secure)) { using (DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry)) { adsSearcher.Filter
= "(sAMAccountName=" + userName + ")"; try { SearchResult adsSearchResult = adsSearcher.FindOne(); } catch (Exception ex) { // 使用者名稱或密碼錯誤 string
strError = ex.Message; } finally { adsEntry.Close(); } } } } } }