asp.net - Parsing in C#, or possibly a tweak to Global Group detector -


i'm trying run through list of global groups user belongs to, , determine whether or not belong specific group. i've got code found somewhere (possibly here) few minor tweaks:

using system; using system.collections.generic; using system.directoryservices; using system.linq; using system.text; using system.web; using system.web.ui; using system.web.ui.webcontrols;  namespace clientdpl {     public partial class webformx : page       {         protected void page_load(object sender, eventargs e)           {              string strusername = system.web.httpcontext.current.user.identity.name;             string username = strusername.substring(strusername.indexof('\\') + 1);             string myglobalgroup = "l_wdjack127_wdc_ssis_user_ch";              list<string> usernestedmembership = new list<string>();                directoryentry domainconnection = new directoryentry(); // use query default domain             //directoryentry domainconnection = new directoryentry("ldap://example.com", "username", "password"); // use query remote domain              directorysearcher samsearcher = new directorysearcher();                samsearcher.searchroot = domainconnection;               samsearcher.filter = "(samaccountname=" + username + ")";               samsearcher.propertiestoload.add("displayname");                searchresult samresult = samsearcher.findone();                if (samresult != null)               {                   directoryentry theuser = samresult.getdirectoryentry();                   theuser.refreshcache(new string[] { "tokengroups" });                    foreach (byte[] resultbytes in theuser.properties["tokengroups"])                   {                       system.security.principal.securityidentifier mysid = new system.security.principal.securityidentifier(resultbytes, 0);                        directorysearcher sidsearcher = new directorysearcher();                        sidsearcher.searchroot = domainconnection;                       sidsearcher.filter = "(objectsid=" + mysid.value + ")";                       sidsearcher.propertiestoload.add("distinguishedname");                        searchresult sidresult = sidsearcher.findone();                        if (sidresult != null)                       {                           usernestedmembership.add((string)sidresult.properties["distinguishedname"][0]);                       }                   }                  int x = 0;                  foreach (string myentry in usernestedmembership)                   {                       //console.writeline(myentry);                     x = x + 1;                     if (myentry == myglobalgroup)                     {                         scriptmanager.registerstartupscript(btnsubmitwork, typeof(button), "data entry", "alert('you member of ' + myentry)", true);                         return;                     }                 }                }               else              {                   console.writeline("the user doesn't exist");               }                //console.readkey();            }          protected void btnsubmitwork_click(object sender, eventargs e)         {         }     } } 

this runs fine. however, when part:

foreach (string myentry in usernestedmembership)   {       //console.writeline(myentry);     x = x + 1;     if (myentry == myglobalgroup)     {         scriptmanager.registerstartupscript(btnsubmitwork, typeof(button), "data entry", "alert('you member of ' + myentry)", true);         return;     } }  

myentry giving me looks this:

cn=l_wdjack127_wdc_ssis_user_ch,ou=alosup,ou=infra,dc=internal, dc=mycompany,dc=com

what need cn piece, or

l_wdjack127_wdc_ssis_user_ch

is there way extract piece string, or tweak code myentry returns 1 bit of information?

you can use regex , grouping extract information such string :

using system.text.regularexpressions;  string pattern = @"(cn)\=(.+)\,";  matchcollection matches = regex.matches(input, pattern); foreach (match match in matches) {     console.writeline(match.groups[1].value); } 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -