【问题标题】:How do I authenticate against Active Directory using ASP.NET Web Pages with Razor?如何使用带有 Razor 的 ASP.NET 网页对 Active Directory 进行身份验证?
【发布时间】:2015-02-05 19:37:13
【问题描述】:

我正在寻找一个带有 Razor 语法的示例,该示例将放入 ASP.NET 网页的 cshtml 文件中。我正在寻找的解决方案将排除在 Visual Studio 中编写 C# 类。我们的项目不是 MVC 项目,而是网页项目。

我需要提示用户输入其内部网 Web 应用的用户名和密码,并使用 LDAP 查询针对 Active Directory 对他们进行身份验证。

例如,我希望 Web Matrix 中有一个内置的帮助程序,但我忽略了(例如,像 WebSecurity 帮助程序,但不是查询数据库,而是查询 AD 服务器)。

【问题讨论】:

  • 到目前为止,您尝试了什么或获得了什么......?你熟悉PrincipalContext ..在这里你可以找到很多工作示例C# PrincipalContext Examples
  • 我用+Razor Active Directory ASP.NET 搜索了谷歌,这是第二次点击:forums.asp.net/t/…
  • 我正在寻找的解决方案将排除在 Visual Studio 中编写 C# 类。例如,我希望 Web Matrix 中有一个内置的 Helper(例如 WebSecurity helper)。也许它不存在,我需要使用 C#/VS 的示例之一自己创建一个 Helper,然后将其导入到 Web Pages 项目中。

标签: c# asp.net razor active-directory ldap


【解决方案1】:

知道了。在 Web Matrix 中,打开 NuGet 包管理器并安装 Novell.Directory.Ldap 包。这是一个示例用法:

@using Novell.Directory.Ldap;
@{
    Validation.RequireField("username", "username is required");
    Validation.RequireField("password", "password is required");

    var username = "";
    var userdn   = "";
    var domain = "domain.com";
    var pwd = "";
    var error = "";

    if (IsPost) {
        if (Validation.IsValid()) {
            username = Request["username"];
            userdn   = "cn=" + username + ",ou=Users,o=INTRANET";
            pwd      = Request["password"];

            try {
                LdapConnection ldapConnection = new LdapConnection();
                ldapConnection.Connect(domain, 389);
                ldapConnection.Bind(userdn, pwd);
            }
            catch (LdapException e) {
                error = e.LdapErrorMessage;
            }
        }
    }
}

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Hello LDAP Page</title>
    </head>
    <body>
        <h1>Hello LDAP Page</h1>
        @if (IsPost) {
            if (error.IsEmpty()) {
                <p>'@username' was successfully authenticated against <i>@domain</i>!</p>
            }
            else {
                <p>error! @error</p>
            }
        }
        else {
            @Html.ValidationSummary()
            <form method="post">
            <fieldset>
                <legend>Login</legend>
                <p><label for="username">Username:</label>
                    <input type="text" name="username" value="" />
                    @Html.ValidationMessage("title")</p>

                <p><label for="password">Password:</label>
                    <input type="password" name="password" value="" />
                    @Html.ValidationMessage("password")</p>

                <p><input type="submit" name="buttonSubmit" value="Login" /></p>
            </fieldset>
            </form> 
        }

    </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    相关资源
    最近更新 更多