protected void btnOk_Click(object sender, EventArgs e)
 {
     try
     {
         List<ADObject> selectedADObjects = new List<ADObject>();
         foreach (GridViewRow gvr in this.gvLDAP.Rows)
         {
             bool selected = ((CheckBox)gvr.Cells[0].FindControl("chkSelect")).Checked;
             if (selected)
             {
                 ADObject ado = new ADObject();
                 string accountName = gvr.Cells[1].Text.Trim(); //SAMAccountName
                 if (String.IsNullOrEmpty(accountName))
                     accountName = gvr.Cells[2].Text.Trim(); //Name
                 ado.Name = accountName;
                 ado.ClassName = gvr.Cells[3].Text;
                 ado.internalSid = new SecurityIdentifier(gvr.Cells[4].Text);
                 ado.ADSPath = gvr.Cells[5].Text;
                 selectedADObjects.Add(ado);
             }
         }
         if (selectedADObjects.Count == 0)
             selectedADObjects = null;
         this.Session["selectedADObjectsFromList"] = selectedADObjects;
         this.closeWindow(true);
     }
     catch (Exception ex)
     {
         this.ShowError(ex.Message);
     }
 }
 protected void Page_Load(object sender, EventArgs e)
 {
     this.selectedAdObjects = (List<ADObject>)this.Session["selectedADObjects"];
     foreach (ADObject ado in this.selectedAdObjects)
     {
         if (ado.state != ADObjectState.Resolved)
         {
             this.adoToResolve = ado;
             break;
         }
     }
     if (!Page.IsPostBack)
     {
         if (adoToResolve.state == ADObjectState.NotFound)
         {
             this.lblMessage.Text = "Unknown Windows User/Group:";
             this.Text = "Unable to find: " + adoToResolve.Name;
             this.txtUnknow.Text = HttpUtility.HtmlEncode(this.adoToResolve.Name);
             this.txtUnknow.Focus();
         }
         else if (adoToResolve.state == ADObjectState.Multiple)
         {
             this.lblMessage.Text = "Ambiguous name:";
             this.Text = "Ambiguous name: " + HttpUtility.HtmlEncode(this.adoToResolve.Name);
             this.txtUnknow.Text = HttpUtility.HtmlEncode(this.adoToResolve.Name);
             this.RefreshActiveDirectoryObjectsList();
         }
         this.Title = this.Text;
         this.Description = this.Text;
     }
 }
Ejemplo n.º 3
0
        private ADObject resolveName(string name)
        {
            name = name.Trim();
            DirectoryEntry    root     = Utility.NewDirectoryEntry("LDAP://" + SqlAzManStorage.RootDSEPath);
            DirectorySearcher deSearch = new DirectorySearcher(root);

            //Try find exactly
            if (this.adObjectType == ADObjectType.UsersOnly || this.adObjectType == ADObjectType.OneUserOnly)
            {
                deSearch.Filter = String.Format("(&(|(displayName={0})(samaccountname={0})(userprincipalname={0})(objectSid={0}))(&(objectClass=user)(objectCategory=person)))", name);
            }
            else if (this.adObjectType == ADObjectType.UsersAndGroups)
            {
                deSearch.Filter = String.Format("(&(|(displayName={0})(samaccountname={0})(userprincipalname={0})(objectSid={0}))(|(&(objectClass=user)(objectCategory=person))(objectClass=group)))", name);
            }

            SearchResultCollection results = deSearch.FindAll();
            ADObject ado = new ADObject();

            try
            {
                //Try find exactly
                if (results.Count == 1)
                {
                    DirectoryEntry de = results[0].GetDirectoryEntry();
                    ado.Name        = (string)de.InvokeGet("samaccountname");
                    ado.ADSPath     = de.Path;
                    ado.UPN         = (string)de.InvokeGet("userPrincipalName");
                    ado.internalSid = new SecurityIdentifier((byte[])de.Properties["objectSid"][0], 0);
                    ado.state       = ADObjectState.Resolved;
                    return(ado);
                }
                //Then try find with jolly (*)
                if (this.adObjectType == ADObjectType.UsersOnly || this.adObjectType == ADObjectType.OneUserOnly)
                {
                    deSearch.Filter = String.Format("(&(|(displayName=*{0}*)(samaccountname=*{0}*)(userprincipalname=*{0}*))(&(objectClass=user)(objectCategory=person)))", name);
                }
                else if (this.adObjectType == ADObjectType.UsersAndGroups)
                {
                    deSearch.Filter = String.Format("(&(|(displayName=*{0}*)(samaccountname=*{0}*)(userprincipalname=*{0}*))(|(&(objectClass=user)(objectCategory=person))(objectClass=group)))", name);
                }
                results = deSearch.FindAll();
                if (results.Count == 0)
                {
                    //Check for Well Know Sid
                    try
                    {
                        NTAccount          nta = new NTAccount(name);
                        SecurityIdentifier sid = (SecurityIdentifier)nta.Translate(typeof(SecurityIdentifier));
                        nta             = (NTAccount)sid.Translate(typeof(NTAccount));
                        ado.Name        = nta.Value;
                        ado.ADSPath     = String.Format("LDAP://<SID={0}>", sid.Value);
                        ado.UPN         = nta.Value;
                        ado.internalSid = sid;
                        ado.state       = ADObjectState.Resolved;
                        return(ado);
                    }
                    catch { }
                    ado.Name  = name;
                    ado.state = ADObjectState.NotFound;
                    return(ado);
                }
                else
                {
                    List <ADObject> proposedADObjects = new List <ADObject>();
                    foreach (SearchResult sr in results)
                    {
                        DirectoryEntry de       = sr.GetDirectoryEntry();
                        ADObject       proposal = new ADObject();
                        proposal.Name        = (string)de.InvokeGet("samaccountname");
                        proposal.ADSPath     = de.Path;
                        proposal.ClassName   = de.SchemaClassName;
                        proposal.UPN         = (string)de.InvokeGet("userPrincipalName");
                        proposal.internalSid = new SecurityIdentifier((byte[])de.Properties["objectSid"][0], 0);
                        proposedADObjects.Add(proposal);
                        this.Session["proposedADObjects"] = proposedADObjects;
                    }
                    ado.Name  = name;
                    ado.state = ADObjectState.Multiple;
                    return(ado);
                }
            }
            catch
            {
                return(ado);
            }
        }
        private ADObject resolveName(string name)
        {
            name = name.Trim();
            DirectoryEntry root = Utility.NewDirectoryEntry("LDAP://" + SqlAzManStorage.RootDSEPath);
            DirectorySearcher deSearch = new DirectorySearcher(root);
            //Try find exactly
            if (this.adObjectType == ADObjectType.UsersOnly || this.adObjectType == ADObjectType.OneUserOnly)
            {
                deSearch.Filter = String.Format("(&(|(displayName={0})(samaccountname={0})(userprincipalname={0})(objectSid={0}))(&(objectClass=user)(objectCategory=person)))", name);
            }
            else if (this.adObjectType == ADObjectType.UsersAndGroups)
            {
                deSearch.Filter = String.Format("(&(|(displayName={0})(samaccountname={0})(userprincipalname={0})(objectSid={0}))(|(&(objectClass=user)(objectCategory=person))(objectClass=group)))", name);
            }

            SearchResultCollection results = deSearch.FindAll();
            ADObject ado = new ADObject();
            try
            {
                //Try find exactly
                if (results.Count == 1)
                {
                    DirectoryEntry de = results[0].GetDirectoryEntry();
                    ado.Name = (string)de.InvokeGet("samaccountname");
                    ado.ADSPath = de.Path;
                    ado.UPN = (string)de.InvokeGet("userPrincipalName");
                    ado.internalSid = new SecurityIdentifier((byte[])de.Properties["objectSid"][0], 0);
                    ado.state = ADObjectState.Resolved;
                    return ado;
                }
                //Then try find with jolly (*)
                if (this.adObjectType == ADObjectType.UsersOnly || this.adObjectType == ADObjectType.OneUserOnly)
                {
                    deSearch.Filter = String.Format("(&(|(displayName=*{0}*)(samaccountname=*{0}*)(userprincipalname=*{0}*))(&(objectClass=user)(objectCategory=person)))", name);
                }
                else if (this.adObjectType == ADObjectType.UsersAndGroups)
                {
                    deSearch.Filter = String.Format("(&(|(displayName=*{0}*)(samaccountname=*{0}*)(userprincipalname=*{0}*))(|(&(objectClass=user)(objectCategory=person))(objectClass=group)))", name);
                }
                results = deSearch.FindAll();
                if (results.Count == 0)
                {
                    //Check for Well Know Sid
                    try
                    {
                        NTAccount nta = new NTAccount(name);
                        SecurityIdentifier sid = (SecurityIdentifier)nta.Translate(typeof(SecurityIdentifier));
                        nta = (NTAccount)sid.Translate(typeof(NTAccount));
                        ado.Name = nta.Value;
                        ado.ADSPath = String.Format("LDAP://<SID={0}>", sid.Value);
                        ado.UPN = nta.Value;
                        ado.internalSid = sid;
                        ado.state = ADObjectState.Resolved;
                        return ado;
                    }
                    catch { }
                    ado.Name = name;
                    ado.state = ADObjectState.NotFound;
                    return ado;
                }
                else
                {
                    List<ADObject> proposedADObjects = new List<ADObject>();
                    foreach (SearchResult sr in results)
                    {
                        DirectoryEntry de = sr.GetDirectoryEntry();
                        ADObject proposal = new ADObject();
                        proposal.Name = (string)de.InvokeGet("samaccountname");
                        proposal.ADSPath = de.Path;
                        proposal.ClassName = de.SchemaClassName;
                        proposal.UPN = (string)de.InvokeGet("userPrincipalName");
                        proposal.internalSid = new SecurityIdentifier((byte[])de.Properties["objectSid"][0], 0);
                        proposedADObjects.Add(proposal);
                        this.Session["proposedADObjects"] = proposedADObjects;
                    }
                    ado.Name = name;
                    ado.state = ADObjectState.Multiple;
                    return ado;
                }
            }
            catch
            {
                return ado;
            }
        }