Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This works for me.</p> <p>You should be able to differentiate between different users on different domain controllers (ie domain/username) because the ldappaths will be different. And according to you you don't care because you are <a href="https://stackoverflow.com/questions/192366/how-to-grab-ad-credentials-from-client-machine-in-a-web-application#192414">not specifying an ldappath</a>.</p> <p>You are making a bid deal about stripping out the domain/ of User.Identity.Name. but I'm not sure what you are worried about, you just need to chop the string into two halves the and the first time you encounter '\' it's time to chop.</p> <p>and if you don't like that you can use the "proper way": <a href="http://msdn.microsoft.com/en-us/library/ms973834.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms973834.aspx</a></p> <p>this is good too <a href="http://geekswithblogs.net/mhamilton/archive/2005/09/30/55621.aspx" rel="nofollow noreferrer"><a href="http://geekswithblogs.net/mhamilton/archive/2005/09/30/55621.aspx" rel="nofollow noreferrer">http://geekswithblogs.net/mhamilton/archive/2005/09/30/55621.aspx</a></a></p> <pre><code> /// This is some imaginary code to show you how to use it Session["USER"] = User.Identity.Name.ToString(); Session["LOGIN"] = RemoveDomainPrefix(User.Identity.Name.ToString()); // not a real function :D string ldappath = "LDAP://your_ldap_path"; // "LDAP://CN=&lt;group name&gt;, CN =&lt;Users&gt;, DC=&lt;domain component&gt;, DC=&lt;domain component&gt;,..." Session["cn"] = GetAttribute(ldappath, (string)Session["LOGIN"], "cn"); Session["displayName"] = GetAttribute(ldappath, (string)Session["LOGIN"], "displayName"); Session["mail"] = GetAttribute(ldappath, (string)Session["LOGIN"], "mail"); Session["givenName"] = GetAttribute(ldappath, (string)Session["LOGIN"], "givenName"); Session["sn"] = GetAttribute(ldappath, (string)Session["LOGIN"], "sn"); /// working code public static string GetAttribute(string ldappath, string sAMAccountName, string attribute) { string OUT = string.Empty; try { DirectoryEntry de = new DirectoryEntry(ldappath); DirectorySearcher ds = new DirectorySearcher(de); ds.Filter = "(&amp;(objectClass=user)(objectCategory=person)(sAMAccountName=" + sAMAccountName + "))"; SearchResultCollection results = ds.FindAll(); foreach (SearchResult result in ds.FindAll()) { OUT = GetProperty(result, attribute); } } catch (Exception t) { // System.Diagnostics.Debug.WriteLine(t.Message); } return (OUT != null) ? OUT : string.Empty; } public static string GetProperty(SearchResult searchResult, string PropertyName) { if (searchResult.Properties.Contains(PropertyName)) { return searchResult.Properties[PropertyName][0].ToString(); } else { return string.Empty; } } </code></pre> <p>For domain/username</p> <pre><code> public static string GetDomain(string s) { int stop = s.IndexOf("\\"); return (stop &gt; -1) ? s.Substring(0, stop + 1) : null; } public static string GetLogin(string s) { int stop = s.IndexOf("\\"); return (stop &gt; -1) ? s.Substring(stop + 1, s.Length - stop - 1) : null; } </code></pre> <p>For username@domain style </p> <pre><code> public static string GetDomain(string s) //untested { int stop = s.IndexOf("@"); return (stop &gt; -1) ? s.Substring(stop + 1, s.Length - stop - 1) : null; } public static string GetLogin(string s) //untested { int stop = s.IndexOf("@"); return (stop &gt; -1) ? s.Substring(0, stop) : null; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload