首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ldap中验证用户身份

在ldap中验证用户身份
EN

Stack Overflow用户
提问于 2012-08-14 20:51:50
回答 2查看 1.1K关注 0票数 0

目录结构类似于,

代码语言:javascript
复制
ou=system,ou=valeteck,cn=mayank

我必须检查用户输入的密码是否正确,并与用户的密码匹配,即mayank。

但是system和cn='mayank'有不同的密码。如果我用cn的口令创建目录条目对象,我没有通过ldap的认证,但是如果我使用系统目录和它的口令,我得到了认证,但是如何检查用户的口令。

EN

回答 2

Stack Overflow用户

发布于 2012-08-14 20:58:42

代码语言:javascript
复制
 private bool LoginS(string userName, string password)
        {
            bool authentic = false;
            try
            {
                DirectoryEntry entry = new DirectoryEntry(LDAP-Path, userName, password, AuthenticationTypes.Secure);
                authentic = true;


                Console.WriteLine("Authentication successful");

            }
            catch (DirectoryServicesCOMException e)
            {
                _logger.Error("Authentification error", e);
                //User doesnt exist or input is false

            }
            return authentic;
        }
票数 0
EN

Stack Overflow用户

发布于 2012-08-14 21:04:19

Windows API甚至还提供了一种更简单的方法,使用advapi32.dll。

示例:

代码语言:javascript
复制
[DllImport("advapi32.dll", EntryPoint = "LogonUserW", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern bool LogOnUser(string lpszUserName, string lpszDomain, string lpszPassword,
        int dwLogOnType, int dwLogOnProvider, ref IntPtr phToken);

如果用户确实在域中并且输入了正确的密码,则此方法将返回true或false。然后,您只需创建自己的登录方法,根据advapi32.dll检查身份验证即可。

代码语言:javascript
复制
public ActionResult SignIn(SignInModel model)
    {
        string domainName = CheckSignIn.GetDomainName(model.User.UserName);
        string userName = CheckSignIn.GetUserName(model.User.UserName);
        IntPtr token = IntPtr.Zero;
        bool result = CheckSignIn.LogOnUser(userName, domainName, model.User.UniqueUserCode, 2, 0, ref token);
        if (result)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]) && Request.QueryString["ReturnUrl"] != "/")
            {
                FormsAuthentication.RedirectFromLoginPage(model.User.UserName, false);
            }
            else
            {
                FormsAuthentication.SetAuthCookie(model.User.UserName, false);
                return RedirectToAction("MyVoyages", "Voyage");
            }
        }
        return SignIn(true);
    }

简单,但功能强大。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11952635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档