在C#中,如何在网络计算机上对用户进行身份验证?

Ale*_*dru 8 c# authentication login

在C#中,如何在网络计算机上对用户进行身份验证?例如,我想从与网络连接的另一台机器在机器上testuser使用密码验证用户.例如,我在,我想验证与上.testpasswordEXAMPLEMACHINEEXAMPLEMACHINEMYMACHINEtestusertestpasswordEXAMPLEMACHINE

我尝试了以下但它一直告诉我,LDAP服务器不可用:

PrincipalContext context =
    new PrincipalContext(ContextType.Domain, exampleMachineDomain);
return context.ValidateCredentials(username, password);
Run Code Online (Sandbox Code Playgroud)

Met*_*Man 2

如果您使用 Active Directory,则可以使用如下内容:

using System.Security;
using System.DirectoryServices.AccountManagement;
    public struct Credentials
    {
        public string Username;
        public string Password;
    }
    public class Domain_Authentication
    {
        public Credentials Credentials;
        public string Domain;
        public Domain_Authentication(string Username, string Password, string SDomain)
        {
            Credentials.Username = Username;
            Credentials.Password = Password;
            Domain = SDomain;
        }
        public bool IsValid()
        {
            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, Domain))
            {
                // validate the credentials
                return pc.ValidateCredentials(Credentials.Username, Credentials.Password);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果您使用的是 Active Directory,则可以使用如下内容:

 PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

    // define a "query-by-example" principal - here, we search for a UserPrincipal 
    UserPrincipal qbeUser = new UserPrincipal(ctx);

    // if you're looking for a particular user - you can limit the search by specifying
    // e.g. a SAMAccountName, a first name - whatever criteria you are looking for
    qbeUser.SamAccountName = "johndoe";

    // create your principal searcher passing in the QBE principal    
    PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

    // find all matches
    foreach(var found in srch.FindAll())
    {
        // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
    }
Run Code Online (Sandbox Code Playgroud)