From 55105066dbe581292614039219c3688b87c69d2b Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 8 Mar 2023 14:21:04 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=8A=A0=E5=85=A5AD=E5=B8=B3=E5=AF=86?= =?UTF-8?q?=E7=99=BB=E5=85=A5=E6=B8=AC=E8=A9=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/BLL/ADUserNameController.cs | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 AMESCoreStudio.WebApi/Controllers/BLL/ADUserNameController.cs diff --git a/AMESCoreStudio.WebApi/Controllers/BLL/ADUserNameController.cs b/AMESCoreStudio.WebApi/Controllers/BLL/ADUserNameController.cs new file mode 100644 index 00000000..79835902 --- /dev/null +++ b/AMESCoreStudio.WebApi/Controllers/BLL/ADUserNameController.cs @@ -0,0 +1,55 @@ +using Microsoft.AspNetCore.Mvc; +using System.DirectoryServices; +using System; + +namespace AMESCoreStudio.WebApi.Controllers.BLL +{ + /// + /// AD 帳號登入 + /// + [Route("api/[controller]")] + [ApiController] + public class ADUserNameController : Controller + { + /// + /// AD帳密 + /// + /// AD伺服器名稱 + /// 登入者帳號 + /// 登入者密碼 + /// + [HttpGet("GetADUser")] + [Produces(contentType: "application/json")] + + public string GetADUserNet(string ADName, string UserName, string UserPwd) + { + string Msg = ""; + try + { + System.DirectoryServices.DirectoryEntry de = new System.DirectoryServices.DirectoryEntry("LDAP://" + ADName, UserName, UserPwd); + System.DirectoryServices.PropertyCollection pc = de.Properties; + if (pc == null) + { + Msg += "登入失敗 "; + } + else + { + Msg += "登入成功 "; + + System.DirectoryServices.DirectorySearcher search = new DirectorySearcher(de); + search.Filter = "(SAMAccountName=" + UserName + ")"; + search.PropertiesToLoad.Add("displayname"); + search.PropertiesToLoad.Add("department"); + SearchResult result = search.FindOne(); + Msg += " 姓名:" + (string)result.Properties["displayname"][0]; + Msg += " 部門:" + (string)result.Properties["department"][0]; + } + return Msg; + } + catch (Exception ex) + { + return ex.Message; + } + } + } +}