2015-09-09 22 views
5

私のASP.NET MVCアプリケーションのActive Directory認証を試してみます。私はSystem.DirectoryServicesを使い、ログイン中にUserManagerのuserを見つけます。ユーザーが見つからない場合は、Active Directoryでユーザーを検索しようとしていますが、成功した場合は、UserManager.CreateAsync()を使用してasp.net mvcアプリケーションにユーザーを登録してください。Asp.NET IDを使用したLDAP認証

private ApplicationUserManager _userManager; 
    private ApplicationRoleManager _roleManager; 

    // 
    // POST: /Account/Login 
    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Login(LoginViewModel loginModel, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = await UserManager.FindAsync(loginModel.UserName, loginModel.Password); 
      if (user != null) 
      { 
       await SignInAsync(user, loginModel.RememberMe); 
       return RedirectToLocal(returnUrl); 
      } 

      string userFullName; 
      if (AuthenticateActiveDirectoryUser("mydomain.local", loginModel.UserName, loginModel.Password, out userFullName)) 
      { 
       var newUser = new ApplicationUser { UserName = loginModel.UserName, FullName = userFullName }; 
       var result = await UserManager.CreateAsync(newUser, loginModel.Password);     

       if (result.Succeeded) 
       { 
        await SignInAsync(newUser, loginModel.RememberMe); 
        return RedirectToLocal(returnUrl); 
       } 

       AddErrors(result); 
      } 
      else 
      { 
       ModelState.AddModelError("", "Invalid UserName or Password"); 
      } 
     } 

     return View(loginModel); 
    } 

    private bool AuthenticateActiveDirectoryUser(
     string domain, 
     string username, 
     string password, 
     out string fullName) 
    { 
     fullName = string.Empty; 

     var domainAndUsername = string.Format("{0}\\{1}", domain, username); 
     var ldapPath = ""; 
     var entry = new DirectoryEntry(ldapPath, domainAndUsername, password); 
     try 
     { 
      // Bind to the native AdsObject to force authentication. 
      var obj = entry.NativeObject; 
      var search = new DirectorySearcher(entry) { Filter = "(SAMAccountName=" + username + ")" }; 
      search.PropertiesToLoad.Add("cn"); 
      var result = search.FindOne(); 
      if (result == null) 
       return false; 

      try 
      { 
       fullName = (string)result.Properties["cn"][0]; 
      } 
      catch 
      { 
       fullName = string.Empty; 
      } 
     } 
     catch (Exception ex) 
     { 
      return false; 
     } 

     return true; 
    } 

私の実装では、Active DirectoryアカウントまたはADアカウントのユーザー変更パスワードが削除された場合は無視されます。 自分のコードで手動で確認できますが、Active Directoryユーザーアカウントによる認証を実装するには、ASP.NET IDに他の方法が存在する可能性があります。

答えて

0

これはU

protected bool ActiveDirectoryLogin(string Username, string Password, string Domain) 
{ 
    bool Success = false; 
    //System.DirectoryServices.DirectoryEntry Entry = 
    // new System.DirectoryServices.DirectoryEntry("LDAP://196.15.32.161:389/cn=KFUPM-People,o=KFUPM,dc=kfupm,dc=edu,dc=sa", "uid=" + Username + ",cn=KFUPM-People,o=KFUPM,dc=kfupm,dc=edu,dc=sa", Password, AuthenticationTypes.None); 

    System.DirectoryServices.DirectoryEntry Entry = 
     new System.DirectoryServices.DirectoryEntry("LDAP://ldapmaster.kfupm.edu.sa:389/cn=KFUPM-People,o=KFUPM,dc=kfupm,dc=edu,dc=sa", "uid=" + Username + ",cn=KFUPM-People,o=KFUPM,dc=kfupm,dc=edu,dc=sa", Password,AuthenticationTypes.None); 

    //System.DirectoryServices.DirectoryEntry Entry = 
    // new System.DirectoryServices.DirectoryEntry("LDAP://ldapmaster.kfupm.edu.sa:389/cn=KFUPM-People,o=KFUPM,dc=kfupm,dc=edu,dc=sa", Username , Password, AuthenticationTypes.None); 

    System.DirectoryServices.DirectorySearcher Searcher = new System.DirectoryServices.DirectorySearcher(Entry); 
    //Entry.Username = "uid="+Username + ",cn=KFUPM-People,o=KFUPM,dc=kfupm,dc=edu,dc=sa"; 
    //Entry.Password = Password; 
    //Entry.AuthenticationType = AuthenticationTypes.None; 
    // Searcher.SearchScope = System.DirectoryServices.SearchScope.Subtree; 

    try 
    { 

     Object nat = Entry.NativeObject; 
     Success = true; 
//   System.DirectoryServices.SearchResult Results =  Searcher.FindOne(); 
//   Success = (Results != null); 

    } 
    catch (Exception e) 
    { 
     Success = false; 
    } 

    return Success; 
} 
助けることができるかどうかを確認
関連する問題