web.configを開きます。あなたのActiveDirectoryのためのConnectionString必要なすべての
まず:<membership>
タグへのダウン
<connectionStrings>
...
<add name="ADConnectionString" connectionString=LDAP://*adserver*/DC=*domain* />
...
</connectionStrings>
スクロールします。
<membership defaultProvider="SimpleMembershipProvider">
はその後<providers>
内のADメンバーの新しいプロバイダを追加します:web.configファイルのためのトリックを行う必要があります
<add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider" connectionStringName="ADConnectionString" attributeMapUsername="sAMAccountName" />
あなたのような、<membership>
に設定さdefaultProvider属性を持っていることを確認してください。これで、ログイン時にADユーザーを認証する必要があります。AccountControllerのログインアクションに移動します。まず、ActiveDirectoryを介してユーザーを認証しようとします。PrincipalContext
という便利なクラスがSystem.DirectoryServices.AccountManagement
名前空間にあります。それが失敗した場合は、デフォルトのメンバシッププロバイダを使用する:あなたはのUserPrincipalクラスとActiveDirectoryのユーザーに現在のログに記録されたを得ることができ、あなたの後の要件については
public ActionResult Login(LoginModel model, string returnUrl)
{
try
{
// try to auth user via AD
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
{
if (pc.ValidateCredentials(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, false);
return RedirectToAction("Index", "Home");
}
}
// try the default membership auth if active directory fails
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, false);
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "Login failed");
}
}
catch
{
}
GetErrorsFromModelState();
return View(model);
}
:
using (var context = new PrincipalContext(ContextType.Domain))
{
using (var aduser = UserPrincipal.FindByIdentity(context,IdentityType.SamAccountName, HttpContext.User.Identity.Name))
{
...
}
}
・ホープ、このことができますし、私が逃しませんでした何でも