项目示意图:
设置:使程序集COM可见
![](https://img-my.csdn.net/uploads/201301/23/1358905834_4380.jpg)
设置:为程序集签名
C#类代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
namespace ClassLibrary1
{
public class Class1
{
public string msg;
public string pat;
public bool IsAuthenticated(string domain, string username, string pwd)
{
string domainAndUsername = domain + @"\" + username;
pat = "LDAP://" + domain;
DirectoryEntry entry = new DirectoryEntry(pat, 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.
pat = result.Path;
msg = (string)result.Properties["cn"][0];
}
catch (Exception ex)
{
msg = ex.Message;
return false;
}
return true;
}
}
}
注册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
%>