明白了。在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>
你有什麼試過或者你到目前爲止得到了什麼..?你是否熟悉'PrincipalContext' ..在這裏你可以找到很多工作示例[C#PrincipalContext示例](http://www.google.com) – MethodMan 2015-02-05 19:40:33
我用'+ Razor Active Directory ASP.NET'搜索了Google,這是第二次打擊:http://forums.asp.net/t/1851715.aspx?MVC3+C+using+razor+with+ldap+active+directory+Authentication – 2015-02-05 19:45:58
我正在尋找的解決方案將排除編寫一個C#類在Visual Studio中。例如,我希望在Web Matrix中有一個內置的Helper(例如像WebSecurity助手)。也許它不存在,我需要使用C#/ VS的其中一個示例自己創建一個Helper,然後將其導入到Web Pages項目中。 – lucid 2015-02-05 19:53:02