6

私は2つの方法でアクセスできるWebアプリケーションを構築しています。同じ組織で働いているすべての人は、アクティブディレクトリを使ってアプリケーションにアクセスできます。Active DirectoryまたはメンバーシップデータベースとのMVC 4認証

外部からの誰もが、別のメンバーシップデータベースを介してアプリケーションに参加する必要があります。誰もが自分の役割を持つメンバーシップデータベースにアカウントを持っているはずです。したがって、パスワードとユーザー名を覚えやすくするために広告接続は単なるボーナスです。 私はインターネットを検索しましたが、匹敵する状況は見つかりませんでした。これは私の広告で初めての作業です。

問題を解決する方法を知っている人は、使用できるフレームワークを知っていますか?

現時点では、私はSystem.Web.WebData.SimpleMembershipProviderでメンバーシップ接続を実装しましたが、正常に動作します。

アプリケーションの後半開発では、いくつかの情報を確認するために広告への他の接続も必要ですが、それは別の日の問題です。

ありがとうございました。

答えて

5

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)) 
    { 
     ... 
    } 
} 

・ホープ、このことができますし、私が逃しませんでした何でも

2

指定したユーザー名とパスワードを持つユーザーが有効な場合、このコードはあなたを与えるだろう

public bool ValidateUser(string userName, string password) 
    { 
     bool authenticated = false; 
     string dePath = string.Empty; 
     dePath += DomainController; 
     if (!string.IsNullOrEmpty(BaseDomainName)) 
     { 
      dePath += "/" + BaseDomainName; 
     } 
     try 
     { 
      DirectoryEntry entry = new DirectoryEntry(dePath, userName, password); 
      object nativeObject = entry.NativeObject; 
      authenticated = true; 
     } 
     catch 
     { 
      return false; 
     } 
     return authenticated; 
    } 

あなたがキー

としてのweb.configのappSettingsにドメインコントローラとBaseDomainNameを追加することができます
関連する問題