Example #1
0
        /// <summary>
        /// ユーザーを更新する
        /// </summary>
        /// <param name="ldapUser"></param>
        /// <returns></returns>
        public Task UpdateAsync(LdapUser ldapUser)
        {
            try
            {
                using (LdapContext ldapContext = new LdapContext())
                {
                    ldapContext.Connect();

                    //LDAPから当該ユーザを取得
                    var result = FindByNameAsync(ldapUser.Id).Result;

                    //OUが異なる場合
                    if (ldapUser.Ou != result.Ou)
                    {
                        //オブジェクトを移動
                        ldapUser.SetDistinguishedName();
                        var fromDn = result.DistinguishedName;
                        var toDn   = string.Format("{0}{1}", ldapUser.Ou == null ? "" : "ou=" + ldapUser.Ou + ",", LdapConfig.NamingContext);
                        ldapContext.Context.MoveEntry(fromDn, toDn);
                        //移動後、再取得
                        result = FindByNameAsync(ldapUser.Id).Result;
                    }

                    //渡された内容に更新
                    _CopyLdapUserProperties(ref ldapUser, ref result);
                    ldapContext.Context.Update(result);
                    return(Task.Delay(0));
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Example #2
0
        public Task SetPasswordHashAsync(LdapUser ldapUser, string passwordHash)
        {
            try
            {
                using (LdapContext ldapContext = new LdapContext())
                {
                    //LDAPサーバーへ接続
                    ldapContext.Connect();

                    //パスワードを設定するユーザー情報を取得
                    var ldapUserPwd = ldapContext.Context.Query <LdapUserPassword>()
                                      .SingleOrDefault(x => x.Id == ldapUser.Id);
                    if (ldapUserPwd == null)
                    {
                        //該当ユーザーなし
                        throw new NotFoundLdapUserException(
                                  string.Format("uid: '{0}' is not found.", ldapUser.Id));
                    }

                    //ハッシュ化済みパスワードを設定する
                    ldapUserPwd.Password = passwordHash;
                    ldapContext.Context.Update <LdapUserPassword>(ldapUserPwd);
                    return(Task.Delay(0));
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Example #3
0
        /// <summary>
        /// ユーザー認証を行う
        /// </summary>
        /// <param name="uid">ユーザーID(uid)</param>
        /// <param name="password">パスワード</param>
        /// <returns></returns>
        public async Task <LdapUser> Authenticate(string uid, string password)
        {
            try
            {
                //ユーザー情報を取得
                var ldapUser = await FindByNameAsync(uid);

                //認証
                try
                {
                    using (LdapContext ldapContext = new LdapContext())
                    {
                        //LDAPサーバにユーザ権限で接続
                        ldapContext.Connect(ldapUser.DistinguishedName, password);
                        var result = ldapContext.Context.Query <LdapUser>()
                                     .SingleOrDefault(x => x.Id == uid);
                        //ユーザー情報を返す
                        return(result);
                    }
                }
                catch (System.DirectoryServices.Protocols.LdapException)
                {
                    //認証失敗(クエリ実行時にエラー)
                    return(null);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Example #4
0
 /// <summary>
 /// ユーザーをユーザーID(uid)で検索する
 /// </summary>
 /// <param name="uid">LDAP上のユーザーID(uid)</param>
 /// <returns></returns>
 public Task <LdapUser> FindByNameAsync(string uid)
 {
     try
     {
         //ユーザー情報を取得
         using (LdapContext ldapContext = new LdapContext())
         {
             //LDAPサーバにシステム権限で接続
             ldapContext.Connect();
             //クエリで該当ユーザーを取得
             var result = ldapContext.Context.Query <LdapUser>(SearchScope.Subtree)
                          .SingleOrDefault(x => x.Id == uid);
             if (result == null)
             {
                 throw new NotFoundLdapUserException(
                           string.Format("uid: '{0}' is not found into LDAP server.", uid));
             }
             //ユーザー情報を返す
             return(Task.FromResult <LdapUser>(result));
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Example #5
0
 /// <summary>
 /// OUの一覧を取得する
 /// </summary>
 /// <returns></returns>
 public Task <List <LdapOu> > FindAsync()
 {
     try
     {
         using (LdapContext ldapContext = new LdapContext())
         {
             //LDAPサーバにシステム権限で接続
             ldapContext.Connect();
             //該当ユーザーを取得
             var results = ldapContext.Context.Query <LdapOu>().ToList();
             return(Task.FromResult <List <LdapOu> >(results));
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Example #6
0
        /// <summary>
        /// ユーザーを削除する
        /// </summary>
        /// <param name="ldapUser"></param>
        /// <returns></returns>
        public Task DeleteAsync(LdapUser ldapUser)
        {
            try
            {
                using (LdapContext ldapContext = new LdapContext())
                {
                    ldapContext.Connect();

                    //LDAPから当該ユーザを取得
                    var result = FindByNameAsync(ldapUser.Id).Result;

                    //渡された内容を削除
                    ldapContext.Context.Delete(result.DistinguishedName);
                    return(Task.Delay(0));
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Example #7
0
 /// <summary>
 /// ユーザーを検索し、ユーザーリストを取得する
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public Task <IEnumerable <LdapUser> > SearchAsync(SearchAsyncModel model)
 {
     try
     {
         using (LdapContext context = new LdapContext())
         {
             context.Connect();
             var results = context.Context.Query <LdapUser>()
                           .AsEnumerable() //IQueryable のままだと Containts(部分一致)が使えない
                           .Where(x => model.Id == null ? true : x.Id.Contains(model.Id))
                           .Where(x => model.FirstName == null ? true : x.FirstName.Contains(model.FirstName))
                           .Where(x => model.LastName == null ? true : x.LastName.Contains(model.LastName))
                           .Where(x => model.OrganizationName == null ? true : x.OrganizationName.Contains(model.OrganizationName))
                           .Select(x => x)
                           .ToList();
             return(Task.FromResult <IEnumerable <LdapUser> >(results));
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Example #8
0
 /// <summary>
 /// ユーザーを一意名(dn)で検索する
 /// </summary>
 /// <param name="dn">LDAP上の一意名(dn)</param>
 /// <returns></returns>
 public Task <LdapUser> FindByIdAsync(string dn)
 {
     try
     {
         using (LdapContext ldapContext = new LdapContext())
         {
             //LDAPサーバにシステム権限で接続
             ldapContext.Connect();
             //該当ユーザーを取得
             var result = ldapContext.Context.GetByDN <LdapUser>(dn);
             if (result == null)
             {
                 throw new NotFoundLdapUserException(
                           string.Format("dn: '{0}' is not found into LDAP server.", dn));
             }
             return(Task.FromResult <LdapUser>(result));
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Example #9
0
        /// <summary>
        /// 新しいユーザーを挿入する
        /// </summary>
        /// <param name="ldapUser"></param>
        /// <returns></returns>
        public Task CreateAsync(LdapUser ldapUser)
        {
            try
            {
                //ユーザーIDの重複チェック
                try
                {
                    var duplicateUser = FindByNameAsync(ldapUser.Id);
                    //同じuidが見つかった場合はNG
                    if (duplicateUser != null)
                    {
                        throw new DuplicateLdapUserIdException(
                                  string.Format("uid: '{0}' is already exists.", ldapUser.Id));
                    }
                }
                catch (NotFoundLdapUserException)
                {
                    //同じuidが見つからなければOK
                }

                using (LdapContext ldapContext = new LdapContext())
                {
                    ldapContext.Connect();

                    //ユーザーの一意名(dn)、表示名を設定
                    ldapUser.SetDistinguishedName();
                    ldapUser.SetDisplayName();
                    //追加処理
                    ldapContext.Context.Add(ldapUser);
                    return(Task.Delay(0));
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }