質問編集履歴

6

AdInfo.cs の Id の型を修正

2018/12/05 05:45

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -158,7 +158,7 @@
158
158
 
159
159
  {
160
160
 
161
- public int Id { get; set; }
161
+ public string Id { get; set; }
162
162
 
163
163
  public string Name { get; set; }
164
164
 

5

AdInfo.cs にコンストラクタ追加、AdController.cs の foreach 内修正

2018/12/05 05:45

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -166,6 +166,24 @@
166
166
 
167
167
  public string Department { get; set; }
168
168
 
169
+
170
+
171
+ // 追加:引数を取るコンストラクタを実装
172
+
173
+ public AdInfo (string Id1, string Name1, string Mail1, string Department1)
174
+
175
+ {
176
+
177
+ Id = Id1;
178
+
179
+ Name = Name1;
180
+
181
+ Mail = Mail1;
182
+
183
+ Department = Department1;
184
+
185
+ }
186
+
169
187
  }
170
188
 
171
189
  }
@@ -214,13 +232,11 @@
214
232
 
215
233
 
216
234
 
217
- // ユーザー情報
235
+ // ここから修正
218
-
236
+
219
- List<AdInfo> UserInfo = new List<AdInfo>();
237
+ List<AdInfo> model = new List<AdInfo>();
220
-
221
-
222
-
223
- // 検索結果でループ
238
+
239
+
224
240
 
225
241
  foreach (var user in result
226
242
 
@@ -230,15 +246,23 @@
230
246
 
231
247
  {
232
248
 
249
+ var id = (string)user.Properties["sAMAccountName"].Value;
250
+
233
- // ここで、↓のような属性の値を1件ごとにUserInfoに入れたい
251
+ var name = (string)user.Properties["name"].Value;
234
-
252
+
235
- //var mailAddress = (string)entry.Properties["mail"].Value;
253
+ var mail = (string)user.Properties["mail"].Value;
254
+
255
+ var department = (string)user.Properties["department"].Value;
256
+
257
+
258
+
259
+ model.Add(new AdInfo(id, name, mail, department));
236
260
 
237
261
  }
238
262
 
239
263
 
240
264
 
241
- return View(UserInfo);
265
+ return View(model);
242
266
 
243
267
  }
244
268
 

4

UserList()の追加

2018/12/05 05:16

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -171,3 +171,81 @@
171
171
  }
172
172
 
173
173
  ```
174
+
175
+ AdController.cs
176
+
177
+ ```C#
178
+
179
+ using System.DirectoryServices;
180
+
181
+
182
+
183
+ namespace Web1.Models
184
+
185
+ {
186
+
187
+ public class AdController : Controller
188
+
189
+ {
190
+
191
+ public ActionResult UserList()
192
+
193
+ {
194
+
195
+ var directoryEntry = new DirectoryEntry();
196
+
197
+ var directorySearcher = new DirectorySearcher(directoryEntry);
198
+
199
+
200
+
201
+ // ユーザーID
202
+
203
+ var samAccountName = "ABC12*";
204
+
205
+
206
+
207
+ // LDAP設定、検索
208
+
209
+ directoryEntry.Path = "LDAP://xxx";
210
+
211
+ directorySearcher.Filter = String.Format("(&(objectClass=user)(samAccountName={0}))", samAccountName);
212
+
213
+ var result = directorySearcher.FindAll(); // FindOneからFindAllに変更
214
+
215
+
216
+
217
+ // ユーザー情報
218
+
219
+ List<AdInfo> UserInfo = new List<AdInfo>();
220
+
221
+
222
+
223
+ // 検索結果でループ
224
+
225
+ foreach (var user in result
226
+
227
+ .Cast<SearchResult>()
228
+
229
+ .Select(x => x.GetDirectoryEntry()))
230
+
231
+ {
232
+
233
+ // ここで、↓のような属性の値を1件ごとにUserInfoに入れたい
234
+
235
+ //var mailAddress = (string)entry.Properties["mail"].Value;
236
+
237
+ }
238
+
239
+
240
+
241
+ return View(UserInfo);
242
+
243
+ }
244
+
245
+ }
246
+
247
+ }
248
+
249
+
250
+
251
+ ```

3

AdInfo.csの編集

2018/12/05 00:54

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -164,6 +164,8 @@
164
164
 
165
165
  public string Mail { get; set; }
166
166
 
167
+ public string Department { get; set; }
168
+
167
169
  }
168
170
 
169
171
  }

2

AdInfo.csを追記

2018/12/05 00:39

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
  AdController.cs
28
28
 
29
- ```ここに言語を入力
29
+ ```C#
30
30
 
31
31
  namespace Web1.Models
32
32
 
@@ -139,3 +139,33 @@
139
139
 
140
140
 
141
141
  よろしくお願いします。
142
+
143
+
144
+
145
+ ---
146
+
147
+ ### 追記
148
+
149
+ AdInfo.cs
150
+
151
+ ```C#
152
+
153
+ namespace Web1.Models
154
+
155
+ {
156
+
157
+ public class AdInfo
158
+
159
+ {
160
+
161
+ public int Id { get; set; }
162
+
163
+ public string Name { get; set; }
164
+
165
+ public string Mail { get; set; }
166
+
167
+ }
168
+
169
+ }
170
+
171
+ ```

1

「やりたいこと」に表を追加しました。

2018/12/04 07:54

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -110,7 +110,19 @@
110
110
 
111
111
  - 検索条件をワイルドカード指定(samAccountName = "ABC12*")
112
112
 
113
- - directorySearcher.FindAll()で複数のデータをViewへ渡す
113
+ - directorySearcher.FindAll()で複数のデータをViewへ渡す(↓View画面のイメージ)
114
+
115
+
116
+
117
+ |cn|name|mail|department|
118
+
119
+ |--|--|--|--|
120
+
121
+ |ABC121|suzuki|suzuki@jp|営業|
122
+
123
+ |ABC122|sato|sato@jp|総務|
124
+
125
+ |ABC123|tanaka|tanaka@jp|経理|
114
126
 
115
127
 
116
128