} // end lbtnEmail_Click(object sender, ...) /// <summary> /// Gets the authenticated user's email address from Active Directory. /// </summary> /// <returns>emailaddress</returns> protected string GetNotifyMailRcpAddr() { string objContentMsg = ""; ADrwInfo ADrwinfoO = new ADrwInfo(); ADobjInfo adobjui = new ADobjInfo(); int retval; string objsearchfilter = "user"; string mailto = ""; string authUsr = Request.LogonUserIdentity.Name; if (!string.IsNullOrEmpty(authUsr)) { if (authUsr.Contains('\\')) { string[] tmp = authUsr.Split('\\'); adobjui.ADUserID = tmp[1]; } } var rtnobj = ADrwinfoO.RetObjValues(ref objContentMsg, out retval, objsearchfilter, adobjui); if (retval == 0) { if (!string.IsNullOrEmpty(rtnobj.ADMail)) { mailto = rtnobj.ADMail; } } return(mailto); } // end GetNotifactionRcpAdr()
} // end lbtnAddGroupMembers_Click /// <summary> /// Updates the changes on the group in Active Directory /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lbtnUpdateGroupMembers_Click(object sender, EventArgs e) { var memberDNs = new List <string>(); string objContentMsg = ""; string objContentMsgGrp; int retval; int retvalGrp; string objsearchfilter = "user"; var adrw = new ADrwInfo(); var adMemberIn = new ADobjInfo(); for (int i = 0; i < lstGroupMembers.Items.Count; i++) { adMemberIn.ADUserID = lstGroupMembers.Items[i].Value.ToString(); var adMemberOut = adrw.RetObjValues(ref objContentMsg, out retval, objsearchfilter, adMemberIn); if (!String.IsNullOrEmpty(adMemberOut.ADdistinguishedName)) { memberDNs.Add(adMemberOut.ADdistinguishedName); } } string adGrpUpdIn = ViewState["agGrpSelected"].ToString(); adrw.AddUsersToGroup(out objContentMsgGrp, out retvalGrp, adGrpUpdIn, memberDNs); string appTextError = "", appLanguage = ""; var appText = new System.Collections.Generic.Dictionary <string, string>(); bool foundLangFile = WbVarious.GetAppText(ref appText, ref appTextError, appLanguage); string errorExceptionTxt, operationFinishedtxt; string feedback = ""; appText.TryGetValue("ErrorException", out errorExceptionTxt); appText.TryGetValue("OperationFinished", out operationFinishedtxt); if (retvalGrp != 0) { feedback = errorExceptionTxt; lblAppFeedback.Attributes.Remove("class"); lblAppFeedback.Attributes.Add("class", "labelfeedbad"); } else { feedback = operationFinishedtxt; lblAppFeedback.Attributes.Remove("class"); lblAppFeedback.Attributes.Add("class", "labelfeedgood"); } lblAppFeedback.Text = feedback; Hide_Buttons(); if (WbGetParams.getBValue("WriteUsageLog")) { Update_UsageLog(); } } // end lbtnUpdateGroupMembers_Click
} // end Page_Load /// <summary> /// Checking if logged on user exists and getting the full name and the groups she is memberof for further comparission. /// </summary> /// <param name="authUsrfullName"></param> /// <param name="authUsr"></param> /// <param name="authUsrMemberOf"></param> protected bool AuthUsr_Load(string authUsr, ref List <string> authUsrMemberOf, ref string authUsrfullName) { if (authUsr.Contains('\\')) { string[] tmp = authUsr.Split('\\'); authUsr = tmp[1]; } lblExistUsrFullName.Text = ""; string objContentMsg = ""; try { ADrwInfo ADrwinfoO = new ADrwInfo(); int retval; string objsearchfilter = "user"; ADobjInfo adobjui = new ADobjInfo(); adobjui.ADUserID = authUsr; var rtnObj = ADrwinfoO.RetObjValues(ref objContentMsg, out retval, objsearchfilter, adobjui); if (retval == 0) { authUsrfullName = rtnObj.ADfirstName; if (rtnObj.ADmemberOf.Count > 0) { authUsrMemberOf.Add(authUsr); foreach (string itm in rtnObj.ADmemberOf) { string[] tmp = itm.Split(','); string lstitm = tmp[0].Replace("cn=", "").Replace("CN=", ""); authUsrMemberOf.Add(lstitm); } } return(true); } else { return(false); } } catch (Exception ex) { objContentMsg = ex.Source + ' ' + ex.Message; return(false); } } //end AuthUsr_Load()
/// <summary> /// Multi purpose query of specified AD objects and some of their properties. /// </summary> /// <param name="objContentMsg"></param> /// <param name="retval"></param> /// <param name="objsearchfilter"></param> /// <param name="adobjin"></param> /// <returns>Object that reflects the queried information</returns> public ADobjInfo RetObjValues(ref string objContentMsg, out int retval, string objsearchfilter, ADobjInfo adobjin) { retval = 0; string objfilter = ""; switch (objsearchfilter) { case "userdn": objfilter = "(&(objectClass=User)(!(userAccountControl:" + AccountDisabledFlag + "))(distinguishedName=" + adobjin.ADdistinguishedName + "))"; break; case "user": objfilter = "(&(objectClass=User)(!(userAccountControl:" + AccountDisabledFlag + "))(samAccountName=" + adobjin.ADUserID + "))"; break; case "mail": objfilter = "(&(objectClass=User)(!(userAccountControl:" + AccountDisabledFlag + "))(proxyAddresses=smtp:" + adobjin.ADMail + "))"; break; case "personnelno": objfilter = "(&(objectClass=User)(!(userAccountControl:" + AccountDisabledFlag + "))(extensionAttribute5=" + adobjin.ADpersonnelNumber.ToString() + "))"; break; case "name": objfilter = "(&(objectClass=User)(!(userAccountControl:" + AccountDisabledFlag + "))(name=" + adobjin.ADfullName + "))"; break; case "firstfamilyname": objfilter = "(|" + "(&(objectClass=Contact)(givenName=" + adobjin.ADfirstName + ")(sn=" + adobjin.ADfamilyName + "))" + "(&(objectClass=User)(!(userAccountControl:" + AccountDisabledFlag + "))(givenName=" + adobjin.ADfirstName + ")(sn=" + adobjin.ADfamilyName + "))" + ")"; break; } ADobjInfo ADObj = new ADobjInfo(); try { string[] myProps = new string[] { "distinguishedName", "extensionAttribute1", "sn", "proxyAddresses", "mail", "name", "givenName", "memberOf", "l", "postalCode", "samAccountName", "streetAddress", "extensionAttribute5" }; using (DirectoryEntry entry = new DirectoryEntry(rootDSE)) using (DirectorySearcher mySearcher = new DirectorySearcher(entry, objfilter, myProps)) { using (SearchResultCollection result = mySearcher.FindAll()) { if (result.Count > 0) { string propertyName = "memberOf"; foreach (SearchResult rs in result) { ResultPropertyValueCollection valueCollection = rs.Properties[propertyName]; ADObj.ADmemberOf = new List <string>(); ADObj.ADdistinguishedName = rs.Properties["distinguishedName"][0].ToString(); if (rs.Properties["samAccountName"].Count > 0) { ADObj.ADUserID = rs.Properties["samAccountName"][0].ToString(); } else { ADObj.ADUserID = ""; } if (rs.Properties["name"].Count > 0) { ADObj.ADfullName = rs.Properties["name"][0].ToString(); } else { ADObj.ADfullName = ""; } if (rs.Properties["givenName"].Count > 0) { ADObj.ADfirstName = rs.Properties["givenName"][0].ToString(); } else { ADObj.ADfirstName = ""; } if (rs.Properties["sn"].Count > 0) { ADObj.ADfamilyName = rs.Properties["sn"][0].ToString(); } else { ADObj.ADfamilyName = ""; } if ((String.IsNullOrEmpty(valueCollection.ToString()))) { ADObj.ADmemberOf.Add(""); } else { if (valueCollection.Count > 0) { for (int j = 0; j < valueCollection.Count - 1; j++) { ADObj.ADmemberOf.Add(valueCollection[j].ToString()); } } else { ADObj.ADmemberOf.Add(""); } } if ((String.IsNullOrEmpty(rs.Properties["extensionAttribute1"].ToString()))) { ADObj.ADcompanyKey = ""; } else { if (rs.Properties["extensionAttribute1"].Count > 0) { ADObj.ADcompanyKey = rs.Properties["extensionAttribute1"][0].ToString(); } else { ADObj.ADcompanyKey = ""; } } if ((String.IsNullOrEmpty(rs.Properties["extensionAttribute5"].ToString()))) { ADObj.ADpersonnelNumber = ""; } else { if (rs.Properties["extensionAttribute5"].Count > 0) { ADObj.ADpersonnelNumber = rs.Properties["extensionAttribute5"][0].ToString(); } else { ADObj.ADpersonnelNumber = ""; } } if ((String.IsNullOrEmpty(rs.Properties["streetAddress"].ToString()))) { ADObj.ADstreetAddress = ""; } else { if (rs.Properties["streetAddress"].Count > 0) { ADObj.ADstreetAddress = rs.Properties["streetAddress"][0].ToString(); } else { ADObj.ADstreetAddress = ""; } } if ((String.IsNullOrEmpty(rs.Properties["postalCode"].ToString()))) { ADObj.ADpostalCode = ""; } else { if (rs.Properties["postalCode"].Count > 0) { ADObj.ADpostalCode = rs.Properties["postalCode"][0].ToString(); } else { ADObj.ADpostalCode = ""; } } if ((String.IsNullOrEmpty(rs.Properties["l"].ToString()))) { ADObj.ADCity = ""; } else { if (rs.Properties["l"].Count > 0) { ADObj.ADCity = rs.Properties["l"][0].ToString(); } else { ADObj.ADCity = ""; } } if ((String.IsNullOrEmpty(rs.Properties["mail"].ToString()))) { ADObj.ADMail = ""; } else { if (rs.Properties["mail"].Count > 0) { ADObj.ADMail = rs.Properties["mail"][0].ToString(); } else { ADObj.ADMail = ""; } } } objContentMsg = "ok"; } else { objContentMsg = "Searched object not found: " + adobjin.ADUserID + " " + adobjin.ADfirstName + " " + adobjin.ADfamilyName; } } } retval = 0; } catch (Exception ex) { objContentMsg += ex.Message + "\n" + ex.StackTrace; retval = 1; } return(ADObj); } // end RetObjValues()
} // end GroupMatches_Disply(List<string> groupMatches) /// <summary> /// Gathers the members of the selected group and displays their clear name. /// </summary> protected void GroupMembers_Display() { ViewState["agGrpSelected"] = drpdAuthUsrRWGroups.SelectedValue; string objContentMsg = ""; int retval; int objContentNO; var adgrpin = new ADgroupobjInfo(); adgrpin.DistinguishedName = drpdAuthUsrRWGroups.SelectedValue; var adrw = new ADrwInfo(); var adgrpout = adrw.RetGroupValues(ref objContentMsg, out retval, out objContentNO, adgrpin); if (retval == 0) { var sbMember = new System.Text.StringBuilder(); var memberDetails = new System.Collections.Generic.Dictionary <string, string>(); string objsearchfilter = "userdn"; foreach (var member in adgrpout.Member) { string objContentMsgMem = ""; int retvalMem; var adusrin = new ADobjInfo(); adusrin.ADdistinguishedName = member; var adrwi = new ADrwInfo(); var adusrout = adrwi.RetObjValues(ref objContentMsgMem, out retvalMem, objsearchfilter, adusrin); if ((retvalMem == 0) && (!String.IsNullOrEmpty(adusrout.ADUserID))) { string usrValue = ""; if (String.IsNullOrEmpty(adusrout.ADfullName)) { usrValue = adusrout.ADUserID; } else { usrValue = adusrout.ADfullName; } memberDetails.Add(adusrout.ADUserID, usrValue); } } lstGroupMembers.Items.Clear(); var items = from pair in memberDetails orderby pair.Value ascending select pair; foreach (KeyValuePair <string, string> pair in items) { var itm = new ListItem(pair.Value, pair.Key); lstGroupMembers.Items.Add(itm); } if (String.IsNullOrEmpty(adgrpout.Description)) { lblADGroupsNotes.Text = " "; } else { lblADGroupsNotes.Text = adgrpout.Description; } } else { string foo = "bar"; } } // end GroupMembers_Display()