c# - Acces denied error when trying to save a distribution group -
when running code shown below, getting error states:
an unhandled exception of type 'system.unauthorizedaccessexception' occurred in
system.directoryservices.dll. additional information: access denied.
it happens on line : group.save();
i have used username , password in other scripts relating active directory have sufficient access need do. although, haven't tried in relation system.directoryservices.accountmanagement
library before.
i using visual studio 2013 running in admin mode, there shouldn't problem command prompt not running in it.
if can give me heads why wouldn't using credentials when saving, appreciated.
try { using (principalcontext pc = new principalcontext(contexttype.domain, "domain", "username", "password")) { groupprincipal group = groupprincipal.findbyidentity(pc, "groupname"); dbaccessmanager dbaccess = new dbaccessmanager(); sqlconnection connstring = new sqlconnection(); connstring.connectionstring = system.configuration.configurationmanager.connectionstrings["sqlconnection"].tostring(); list<employee> sqlemployees = dbaccess.readuserinformation(connstring.connectionstring.tostring()); foreach (employee emp in sqlemployees) { userprincipal prinicipaluser = userprincipal.findbyidentity(pc, emp.username); userprincipal aduser = userprincipal.findbyidentity(pc, emp.username); if (prinicipaluser.ismemberof(group) && string.isnullorempty(emp.saplogin)) { group.members.remove(aduser); } else if (!prinicipaluser.ismemberof(group) && !string.isnullorempty(emp.saplogin)) { group.members.add(aduser); } } group.save(); } } catch (directoryservicescomexception e) { console.writeline("exception: " + e.message); }
the problem turned out permissions issue on distribution list
Comments
Post a Comment