在C#中,我如何验证网络机器上的用户? [英] In C#, how do I authenticate a user on a network machine?

查看:118
本文介绍了在C#中,我如何验证网络机器上的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我如何验证网络机器上的用户?例如,我想用户 TESTUSER ,密码 testpassword 在机器上验证 EXAMPLEMACHINE 从不同的机,这是网络连接到 EXAMPLEMACHINE 。例如,我在 MYMACHINE ,我想验证 TESTUSER testpassword EXAMPLEMACHINE

我曾尝试以下,但它总是告诉我, LDAP服务器不可用

  PrincipalContext背景=
    新PrincipalContext(ContextType.Domain,exampleMachineDomain);
返回context.ValidateCredentials(用户名,密码);


解决方案

如果你的不可以使用Active Directory,您可以使用类似这样的:

 使用System.Security;
使用System.DirectoryServices.AccountManagement;
    公共结构证书
    {
        公共字符串用户名;
        公共字符串密码;
    }
    公共类Domain_Authentication
    {
        公共凭证凭证;
        公共字符串域;
        公共Domain_Authentication(用户名字符串,字符串密码,串SDomain)
        {
            Credentials.Username =用户名;
            Credentials.Password =密码;
            域名= SDomain;
        }
        公共BOOL的IsValid()
        {
            使用(PrincipalContext PC =新PrincipalContext(ContextType.Domain,域))
            {
                //验证凭据
                返回pc.ValidateCredentials(Credentials.Username,Credentials.Password);
            }
        }
    }

如果您正在使用Active Directory,您可以使用这样的:

  PrincipalContext CTX =新PrincipalContext(ContextType.Domain);    //定义一个查询通过例如委托 - 在这里,我们寻找一个UserPrincipal
    UserPrincipal qbeUser =新UserPrincipal(CTX);    //如果你正在寻找一个特定的用户 - 您可以限制通过指定搜索
    //例如一个SAM帐户名,第一个名字 - 任何标准您正在寻找
    qbeUser.SamAccountName =人johndoe;    //创建传入QBE主体主要搜索
    PrincipalSearcher SRCH =新PrincipalSearcher(qbeUser);    //找到所有比赛
    的foreach(VAR在srch.FindAll发现())
    {
        //作事在这里 - 发现的类型是主 - 它可以是用户,组,计算机.....
    }

In C#, how do I authenticate a user on a network machine? For example, I want to authenticate the user testuser with password testpassword on the machine EXAMPLEMACHINE from a different machine which is network-connected to EXAMPLEMACHINE. For example, I am on MYMACHINE and I want to authenticate testuser with testpassword on EXAMPLEMACHINE.

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

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

解决方案

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

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:

 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#中,我如何验证网络机器上的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆