Add/remove users to active directory not working

  • Thread starter Thread starter msdnpublic1234
  • Start date Start date
M

msdnpublic1234

Guest
Hi,

I am trying to add/remove users from active directory groups.The sql query results are captured by the dts object and is being used to fill adapter table.I am processing data in each row and depending upon whether Request is Add/Remove,I am calling Add/Remove functions.I dont know what is happening because there are no exceptions /errors thrown.The Add/Remove succeeds without actually adding anyone in the AD.In fact,the log file gets messages printed for users added or removed.

Pls guide.Below is the code:


public void Main()
{
try
{
string connectionString = @"Data Source=SQL1234;Provider=SQLOLEDB;Initial Catalog=ADUsers;Integrated Security=SSPI";
string sql = "UPDATE BI_UserPermissionList SET ProcessedToAD='yes' WHERE RequestType = ? AND ADGroupName LIKE ? AND Submitted_For = ? AND ApprovalStatus = ?;";
using (OleDbConnection cnn = new OleDbConnection(connectionString))
{
cnn.Open();
var context = new PrincipalContext(ContextType.Domain, "xyz123", "svaccount", "abcd");
var table = new DataTable();
using (var adapter = new OleDbDataAdapter())
{
adapter.Fill(table, Dts.Variables["User::Users"].Value);
foreach (DataRow row in table.Rows)
{
string Request = row["RequestType"].ToString();
string RequestFor = row["Submitted_For"].ToString();
string User = row["sAMAccountName"].ToString();
string AD = row["ADGroupName"].ToString();
string Status = row["ApprovalStatus"].ToString();

if (Request == "Add")
{
var groupname = GroupPrincipal.FindByIdentity(context,AD);
AddUserToGroup(context, groupname, User);
groupname.Save();

}
else
{
var groupname = GroupPrincipal.FindByIdentity(context,AD);
RemoveUserFromGroup(context, groupname, User);
groupname.Save();
}

using (OleDbCommand command = new OleDbCommand(sql, cnn))
{
command.Parameters.Add("@req", OleDbType.VarChar).Value = Request;
command.Parameters.AddWithValue("@grp", "%" + AD + "%");
command.Parameters.AddWithValue("@reqFor", RequestFor);
command.Parameters.AddWithValue("@status", Status);

command.ExecuteNonQuery();
}
}
}
}
}

catch (Exception exp)
{
var ErrorFile = (string)Dts.Variables["$Package::ErrorLog"].Value;
Dts.Events.FireError(0, "Standard Permissions App Requests", exp.Message + "\r" + exp.StackTrace, String.Empty, 0);
File.AppendAllText(ErrorFile, "Date :" +
DateTime.Now.ToString() + "\t" + "Message:" + exp.Message + "\t" + Environment.NewLine);

//Dts.TaskResult = (int) ScriptResults.Failure;
}
}


public void AddUserToGroup(PrincipalContext context, GroupPrincipal group, string result)
{
var ErrorFileAdd = (string)Dts.Variables["$Package::ErrorLog"].Value;

var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, result.Trim().ToLower());

if (!group.Members.Contains(user))
{
group.Members.Add(user);

File.AppendAllText(ErrorFileAdd, "Date :" +
DateTime.Now.ToString() + "\t" + "Message:" + "Added this user to Ad:" + "\t" + result + "\t" + group + Environment.NewLine);
}
}

}

public void RemoveUserFromGroup(PrincipalContext context, GroupPrincipal group, string username)
{
var ErrorFileRmv = (string)Dts.Variables["$Package::ErrorLog"].Value;
var person = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username.Trim().ToLower());
if (group.Members.Contains(person))
{
group.Members.Remove(context, IdentityType.SamAccountName, username);

File.AppendAllText(ErrorFileRmv, "Date :" +
DateTime.Now.ToString() + "\t" + "Removed this user from AD:" + username + "\t" + group + Environment.NewLine);
}

}

Continue reading...
 
Back
Top