当前位置:  开发笔记 > 后端 > 正文

如何从Web应用程序中的客户端计算机获取AD凭据?

如何解决《如何从Web应用程序中的客户端计算机获取AD凭据?》经验,为你挑选了1个好方法。

是否可以从Web应用程序中获取客户端计算机上的用户的activedirectory凭据?

为了澄清,我正在设计一个将在客户的Intranet上托管的Web应用程序.

需要在访问应用程序时不提示应用程序的用户提供凭据,而是应该自动抓取登录到客户端计算机的用户的凭据,而无需用户交互.



1> Sean Hanley..:

绝对.这对于Intranet应用程序尤其有用.

既然你没有指定你的环境,我会假设它是.NET,但这当然不是唯一可行的方法.

可以使用LDAP轻松查询Active Directory .如果您使用的是.NET,则可以执行此代码示例或下面的示例.您也可以在SQL环境中执行此操作.

如果您只需要Windows来处理身份验证,则可以设置例如用于Windows身份验证的.NET Web应用程序.请确保为您的应用程序关闭 IIS中的匿名登录.完成后,您将能够访问用户的Windows登录名并使用它进行进一步的安全检查(例如,他们在AD中的组/角色成员身份).

您还可以使用Enterprise Library的安全应用程序块之类的东西来简化整个混乱.


这是一个简短的C#示例:( 在这里转换为VB.NET )

using System.DirectoryServices;

/// 
/// Gets the email address, if defined, of a user from Active Directory.
/// 
/// The userid of the user in question.  Make
/// sure the domain has been stripped first!
/// A string containing the user's email address, or null
/// if one was not defined or found.
public static string GetEmail(string userid)
{
    DirectorySearcher searcher;
    SearchResult result;
    string email;

    // Check first if there is a slash in the userid
    // If there is, domain has not been stripped
    if (!userid.Contains("\\"))
    {
        searcher = new DirectorySearcher();
        searcher.Filter = String.Format("(SAMAccountName={0})", userid);
        searcher.PropertiesToLoad.Add("mail");
        result = searcher.FindOne();
        if (result != null)
        {
            email = result.Properties["mail"][0].ToString();
        }
    }

    return email;
}

您不必指定域控制器.对DirectorySearcher执行empty/default构造函数会导致它尝试自动查找 - 事实上,这是首选方法.

推荐阅读
手机用户2502851955
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有