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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8973591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:24:40  来源:igfitidea点击:

In C#, how do I authenticate a user on a network machine?

c#authenticationlogin

提问by Alexandru

In C#, how do I authenticate a user on a network machine? For example, I want to authenticate the user testuserwith password testpasswordon the machine EXAMPLEMACHINEfrom a different machine which is network-connected to EXAMPLEMACHINE. For example, I am on MYMACHINEand I want to authenticate testuserwith testpasswordon EXAMPLEMACHINE.

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

I have tried the following but it keeps telling me that, The LDAP server is unavailable:

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

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

采纳答案by MethodMan

If you are notusing Active Directory, you could use something like this:

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

If you are using Active Directory, you could use something like this:

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

回答by Christoph Fink

If your machines are not in a domain, you need to use ContextType.Machine:

如果您的机器不在域中,则需要使用ContextType.Machine

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

回答by Joel Martinez

There's a few different options presented in this answer. The snippet you pasted above shouldwork, though.

此答案中提供了几个不同的选项。不过,您上面粘贴的代码段应该可以工作。

回答by Mark Green

The best way to do this would be to use WNetUseConnection, a Win32 API which allows for the most direct way. In effect, you're trying to call

最好的方法是使用 WNetUseConnection,这是一个 Win32 API,它允许最直接的方式。实际上,您正在尝试调用

net use \\server password /user:myUserName,

net use \\server password /user:myUserName,

which is what this API is intended to do.

这就是这个 API 的目的。

A good example of this has been answered at this question.

这个问题上已经回答了一个很好的例子。

Because the PinvokeWindowsNetworkingfunction returns null when successful, the simplest authentication code is

因为PinvokeWindowsNetworking函数成功返回null,所以最简单的验证码是

private static bool AuthenticateUserOnRemote(string server, string userName, string password)
{
    var connected = PinvokeWindowsNetworking.connectToRemote(server, userName, password);
    var disconnected = PinvokeWindowsNetworking.disconnectRemote(server);
    return connected == null;
}