簡體   English   中英

.net Web服務以使用SSL驗證LDAP憑據

[英].net web service to validate LDAP credentials using SSL

因此,我對整個.net還是陌生的,但是基本上我想做的是編寫一個Web服務,該服務將接收帶有用戶名和密碼的SOAP請求,並在ldap服務器上檢查它們以驗證憑據是否正確。 我已經在perl中完成了此操作,代碼在下面,但是我想在.net中復制它,以進一步了解它。 我一直在使用c#,因為我已經做了大量的Java編程,而且看起來它們很相似,但是如果在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:驗證用戶時出錯。 服務器無法運行。

我覺得我只是在某處或類似的地方指定了錯誤的路徑,但我似乎無法弄清楚。

似乎這將創建一個看起來像"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搜索,然后使用用戶名和密碼重新綁定。 是來自ASP.net的一個很好的示例。 但是,該示例希望用戶名采用uid形式,您需要將其更改為SAMAccountName

同樣,您似乎在指定TLS端口,但尚未指定AuthenticationTypes.SecureSocketsLayer ,這可能會導致收到錯誤。

根據您的代碼,將以下內容傳遞給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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM