gpt4 book ai didi

c# - 在 C# 中,如何验证网络计算机上的用户?

转载 作者:太空狗 更新时间:2023-10-29 19:39:45 26 4
gpt4 key购买 nike

在 C# 中,如何验证网络计算机上的用户?例如,我想在机器 EXAMPLEMACHINE 上使用密码 testpassword 验证用户 testuser 来自网络连接到 的另一台机器>示例机器。例如,我在 MYMACHINE 上,我想在 EXAMPLEMACHINE 上使用 testpassword 验证 testuser

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

PrincipalContext context =
new PrincipalContext(ContextType.Domain, exampleMachineDomain);
return context.ValidateCredentials(username, password);

最佳答案

如果您使用 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);
}
}
}

如果您使用的是 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.....
}

关于c# - 在 C# 中,如何验证网络计算机上的用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8973591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com