所以我对整个.net事情都很陌生,但基本上我想要做的就是编写一个Web服务,它将获取带有用户名和密码的SOAP请求,并在ldap服务器上检查它们以验证凭证是对的。我已经在perl中完成了这个,代码在下面,但是我想在.net中复制它以获得更多的曝光。因为我已经完成了大量的java编程,所以我一直在用c#进行调整,看起来它们非常相似,但如果它在vb或其他方面更容易,我不反对。
我遇到的主要问题实际上是连接到服务器本身。我在网上找到了一些例子,但我对于我应该传递给各种方法的内容感到困惑。以下是Web服务的perl版本:
authenticate.cgi
use strict;
use warnings;
use SOAP::Transport::HTTP;
SOAP::Transport::HTTP::CGI
->dispatch_to('Authenticate')
->handle;
package Authenticate;
use Net::LDAPS;
use Net::LDAP;
sub Verify{
my ($class, $user, $pass) = @_;
my $user_dn = "uid=$user,ou=people,o=domain";
my $ldap = Net::LDAPS->new( 'ldap.domain.com', port => '636' ) or die "$@";
my $mesg = $ldap->bind($user_dn, password => $pass);
return $mesg->error if $mesg->is_error;
return "Successful authentication for user '$user'";
}
我在c#中搞砸的是以下内容(我可能有不必要的内容,我一直在搞乱一堆事情):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
using System.Net;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string Authenticate(string username, string pwd) {
string domain = "LDAP://ldap.domain.com:636";
string domainAndUsername = domain + @"\" + username;
string path = domain + @"/ou=people,o=domain";
string filterAttribute;
DirectoryEntry entry = new DirectoryEntry(path, 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.
path = result.Path;
filterAttribute = (string)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return "true";
}
}
这导致了错误
System.Exception:验证用户时出错。服务器无法运行。
我觉得我只是在某处或类似的地方指定路径错误,但我似乎无法弄明白。
答案 0 :(得分:1)
这似乎会创建一个看起来像"LDAP://ldap.domain.com:636\username/ou=people,o=domain"
的路径。通常它是"LDAP://ldap.domain.com:636/cn=Full User Common Name,ou=People,dc=domain,dc=local"
。
如果您没有开始使用DN,则应匿名绑定或使用服务凭据绑定,使用SAMAccountName
进行搜索,然后使用用户名和密码重新绑定。 Here是ASP.net的一个很好的例子。但是,该示例要求用户名为uid
,您希望将其更改为SAMAccountName
此外,您似乎正在指定TLS端口,但尚未指定AuthenticationTypes.SecureSocketsLayer
,这可能会导致您收到的错误。
答案 1 :(得分:1)
根据您的代码,您将以下内容传递给DirectoryEntry
构造函数:
路径:LDAP://ldap.domain.com:636/ou=people,o=domain
用户名:LDAP://ldap.domain.com:636\username
该用户名不正确。如果是Active Directory,您可以放置ldap.domain.com\username
,但它看起来不像是Active Directory。所以你应该给用户的专有名称。从你的perl看起来应该是uid=username,ou=people,o=domain
,但这看起来很奇怪。我总是将其视为cn=username,ou=people,o=domain
。