项目示意图:
设置:使程序集COM可见
设置:为程序集签名
C#类代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.DirectoryServices; 6 namespace ClassLibrary1 7 { 8 public class Class1 9 { 10 public string msg; 11 public string pat; 12 public bool IsAuthenticated(string domain, string username, string pwd) 13 { 14 string domainAndUsername = domain + @"\" + username; 15 pat = "LDAP://" + domain; 16 DirectoryEntry entry = new DirectoryEntry(pat, domainAndUsername, pwd); 17 try 18 { 19 //Bind to the native AdsObject to force authentication. 20 object obj = entry.NativeObject; 21 DirectorySearcher search = new DirectorySearcher(entry); 22 search.Filter = "(SAMAccountName=" + username + ")"; 23 search.PropertiesToLoad.Add("cn"); 24 SearchResult result = search.FindOne(); 25 if (null == result) 26 { 27 return false; 28 } 29 //Update the new path to the user in the directory. 30 pat = result.Path; 31 msg = (string)result.Properties["cn"][0]; 32 } 33 catch (Exception ex) 34 { 35 msg = ex.Message; 36 return false; 37 } 38 39 return true; 40 } 41 } 42 }
注册DLL:
RegAsm ClassLibrary1.dll /codebase
注销DLL:
RegAsm ClassLibrary1.dll /u
注册组件默认位置:C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe
ASP调用已注册成功的动态链接库
<% dim obj,isad set obj = server.CreateObject("ClassLibrary1.Class1") isad = obj.IsAuthenticated("xxx.xxx.xxx","username","password") response.Write(isad&","&obj.pat&","&obj.msg) set obj = nothing %>