【问题标题】:Authenticating another user when using Windows Authentication使用 Windows 身份验证时对其他用户进行身份验证
【发布时间】:2013-08-26 19:54:32
【问题描述】:

我有一个使用 Windows 身份验证的 .NET MVC 4 应用程序。有些用户是管理员,需要能够代表其他用户输入数据。

我有一个文本框,管理员可以在其中输入另一个用户的姓名。如何检查以确认输入的文本是现有的 Windows 用户名?

【问题讨论】:

    标签: c# .net asp.net-mvc-4 windows-authentication


    【解决方案1】:

    您可以使用FindByIdentity 方法:

    string username = "Some username you retrieved from the TextBox";
    
    using (var ctx = new PrincipalContext(ContextType.Domain, "YOUR_DOMAIN"))
    using (var user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, username))
    {
        bool userExists = user != null;
        // here you know whether the user exists or not
    }
    

    【讨论】:

    • 投反对票有什么理由吗?请在对答案投反对票时发表评论,解释您认为该答案错误的原因。
    • 不确定这个问题和这些答案的所有仇恨背后是什么,但这对我有用。谢谢。
    【解决方案2】:

    您可以为此查询您组织的 Active Directory。

    DirectoryEntry entry = new DirectoryEntry("LDAP://DomainName");
    DirectorySearcher Dsearch = new DirectorySearcher(entry);
    String Name="Richmond";
    dSearch.Filter = "(&(objectClass=user)(l=" + Name + "))";
    

    请看这篇文章: http://www.codeproject.com/Articles/6778/How-to-get-User-Data-from-the-Active-Directory

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-17
    • 2012-09-29
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2017-03-11
    • 2013-07-26
    • 1970-01-01
    相关资源
    最近更新 更多