public ActionResult Search()
        {
            ViewBag.Result = String.Empty;
            if (Request.HttpMethod == "POST")
            {
                string           query   = Request.Form["query"];
                NexusLdapService service = new NexusLdapService();

                JToken user = service.SearchByLogin(query);
                if (user != null)
                {
                    ViewBag.Result = user.ToString();
                }
            }

            return(View("~/Views/Nexus/LDAP/Search.cshtml"));
        }
Exemple #2
0
        internal static (User, List <string>) getUserAndHisGroupsFromAD(string userName = null, string identify = null)
        {
            DBEntities context = COREobject.i.Context;

            // split userName & domain
            string serverName;
            string onlyName = null;

            // use userName
            if (!string.IsNullOrWhiteSpace(userName))
            {
                int domainIndex = userName.IndexOf('\\');
                serverName = null;
                onlyName   = userName;
                if (domainIndex != -1)
                {
                    serverName = userName.Substring(0, domainIndex).ToLower();
                    onlyName   = userName.Substring(domainIndex + 1);
                }
            }
            // use identify
            else if (!string.IsNullOrWhiteSpace(identify))
            {
                serverName = getUserServer(identify);
            }
            // nothing
            else
            {
                return(null, null);
            }

            // search in AD
            NexusLdapService search = new NexusLdapService();

            if (serverName != null)
            {
                search.UseServer(serverName);
            }
            JToken ldapResult = (onlyName != null)
                ? search.SearchByLogin(onlyName)
                : search.SearchByIdentify(identify);

            // no user found
            if (ldapResult == null)
            {
                return(null, null);
            }

            // user attributes
            User user = new User
            {
                UserName     = userName ?? $"{getUserServer(identify).ToUpper()}\\{ldapResult["samaccountname"]}",
                DisplayName  = (string)ldapResult["displayname"],
                Email        = (string)ldapResult["mail"],
                Address      = "",
                Company      = "",
                Department   = "",
                Team         = "",
                Job          = (string)ldapResult["title"],
                WorkPhone    = "",
                MobilPhone   = "",
                LastLogin    = DateTime.FromFileTime((long)ldapResult["lastlogon"]),
                CurrentLogin = DateTime.UtcNow,

                ModuleAccessPermission = new ModuleAccessPermission(),

                AuthTypeId     = new MasterAD().Id,
                localExpiresAt = DateTime.UtcNow
            };

            // groups
            List <string> groupNames = new List <string>();

            foreach (JToken group in ldapResult["memberof"])
            {
                string groupIdentify = (string)group;

                int startI = groupIdentify.IndexOf("CN=") + 3;
                int EndI   = groupIdentify.IndexOf(',', startI);
                groupNames.Add(groupIdentify.Substring(startI, EndI - startI));
            }

            return(user, groupNames);
        }