{"id":551,"date":"2016-12-16T13:18:59","date_gmt":"2016-12-16T12:18:59","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=551"},"modified":"2019-04-10T08:46:33","modified_gmt":"2019-04-10T07:46:33","slug":"escape-ldap-search-filter-active-directory","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/escape-ldap-search-filter-active-directory\/","title":{"rendered":"Escape ldap search filter \/ Active Directory"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\/\n\n\/\/\/ Escapes the LDAP search filter to prevent LDAP injection attacks.\n\/\/\/\n\/\/\/The search filter. \npublic static string EscapeLdapSearchFilterParam(string searchFilter) {\n        StringBuilder escape = new StringBuilder();\n        for (int i = 0; i &lt; searchFilter.Length; ++i) {\n                char current = searchFilter[i];\n                switch (current) {\n                case '\\\\': escape.Append(@\"\\5c\"); break;\n                case '*': escape.Append(@\"\\2a\"); break;\n                case '(': escape.Append(@\"\\28\"); break;\n                case ')': escape.Append(@\"\\29\"); break;\n                case '\\u0000': escape.Append(@\"\\00\"); break;\n                case '\/': escape.Append(@\"\\2f\"); break;\n                default: escape.Append(current); break;\n                }\n        }\n        return escape.ToString();\n}\npublic bool Authenticate(string userName, string password) {\n        bool authentic = false;\n        try {\n                DirectoryEntry entry = new DirectoryEntry(this.LdapPath, userName, password);\n                object nativeObject = entry.NativeObject; authentic = true;\n        } catch (DirectoryServicesCOMException) { throw; }\n        return authentic;\n}\npublic void ChangePassword(string userName, string currentPassword, string newPassword) {\n        try {\n                DirectoryEntry directionEntry = new DirectoryEntry(this.LdapPath, userName, currentPassword);\n                if (directionEntry != null) {\n                        DirectorySearcher search = new DirectorySearcher(directionEntry);\n                        search.Filter = \"(SAMAccountName=\" + EscapeLdapSearchFilterParam(userName) + \")\";\n                        SearchResult result = search.FindOne();\n                        if (result != null) {\n                                DirectoryEntry userEntry = result.GetDirectoryEntry();\n                                if (userEntry != null) {\n                                        userEntry.Invoke(\"ChangePassword\", new object[] { currentPassword, newPassword });\n                                        userEntry.CommitChanges();\n                                }\n                        }\n                }\n        } catch (Exception ex) {\n                throw ex;\n        }\n}\npublic void ResetPassword(string userDn, string password) {\n        DirectoryEntry uEntry = new DirectoryEntry(userDn);\n        uEntry.Invoke(\"SetPassword\", new object[] { password });\n        uEntry.Properties[\"LockOutTime\"].Value = 0;\n\n        \/\/unlock account\n        uEntry.Close();\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-551","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=551"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/551\/revisions"}],"predecessor-version":[{"id":2072,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/551\/revisions\/2072"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}