简体   繁体   English

对ActiveDirectory进行身份验证

[英]Authenticate against ActiveDirectory

I did a little Googling and I came accross this promising code 我做了一点谷歌搜索,我遇到了这个有前途的代码

System.DirectoryServices.AccountManagement.PrincipalContext pc = new System.DirectoryServices.AccountManagement.PrincipalContext(ContextType.Domain, "YOURDOMAIN")
// validate the credentials 
bool validatedOnDomain = pc.ValidateCredentials(userName, tb.Text.ToString());

userName is initialized as the Windows login name. userName初始化为Windows登录名。 It's also a string tb.Text.ToString() is the textbox that is being used for typing the password 它也是一个字符串tb.Text.ToString()是用于输入密码的文本框

Updated code and it's working. 更新了代码,它正在运行。 Thanks all 谢谢大家

MSDN says that PrincipalContext can use two arguments MSDN说PrincipalContext可以使用两个参数

Try to figure out this code.. This is working perfectly in my project. 试着找出这个代码..这在我的项目中完美运行。

public bool  ValidateUser(string varDomain, string varUserName, string varPwd)
    {
        Boolean isValidUser;
        using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, varDomain))
        {
            isValidUser = pc.ValidateCredentials(varUserName, varPwd);
        }
        return isValidUser;

    }

type used in a using statement must be implicitly convertible to 'System.IDisposable' using语句中使用的类型必须可以隐式转换为'System.IDisposable'

Means that you need to change your code to: 意味着您需要将代码更改为:

PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");

// validate the credentials 
bool validatedOnDomain = pc.ValidateCredentials(userName, tb.Text.ToString());

Basically it's just telling you that you cannot use a PrincipalContext in a using statement, because PrincipalContext does not implement the interface called IDisposable . 基本上它只是告诉你不能在using语句中使用PrincipalContext ,因为PrincipalContext没有实现名为IDisposable的接口。

EDIT As marc_s has pointed out below, the PrincipalContext you are using is not the right one. 编辑正如marc_s在下面指出的那样,您使用的PrincipalContext不是正确的。 It seems to live in your own namespace. 它似乎存在于您自己的命名空间中。 You should be using that from System.DirectoryServices.AccountManagement . 您应该使用System.DirectoryServices.AccountManagement那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM