Web Service for Update Active Directory: Help Needed!


hi,

i building web service used update active directory.
the information update come join created 2 different tables , databases.

first information join query employeeid wich object ad ad.
my problem totally newby kind of task , having trouble check in join data , match employeeid in ad , update ad.

what need me check code , me fix , make work way needed. i getting close deadline project , really, need this. order book but can wait till it, hope friday wich deadline. 
also need verify error can written in logfile code.

any appreciated. please need this. sad
i've been looking on internet different code , here frankenstein.

web service code:
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.linq;
using system.web;
using system.web.services;
using system.web.services.protocols;
using system.xml.linq;
using system.data.sql;
using system.data.sqltypes;
using system.data.sqlclient;
using system.directoryservices;
using system.io;

namespace activedirectoryservice {
    /// <summary>
    /// active directory web service
    /// gets data xcadusers , finance employee table
    /// </summary>
    [webservice(namespace = "http://myweb.com/")]
    [webservicebinding(conformsto = wsiprofiles.basicprofile1_1)]
    [toolboxitem(false)]
    // allow web service called script, using asp.net ajax, uncomment following line.
    // [system.web.script.services.scriptservice]
    public class service1 : system.web.services.webservice {

        #region variables
        string employeeid = "";
        string fname = "";
        string lname = "";
        string telnum = "";
        string ipphone = "";
        string mobile = "";
        string epager = "";
        string manager = "";
        string dpt = "";
        string initials = "";
        string dispname = "";
        string email = "";
        string homephone = "";
        string title = "";
        string filepath = "";
        string slogformat;
        string serrtime;

        public string connstring = "server=myserver;uid=myusername;pwd=mypass;database=mydb;integrated security=true";

        directoryentry dentry = null;
        directorysearcher ds = null;
        sqlconnection sqlconn = null;
        sqlcommand mycmd = null;
        streamwriter sw = null;
        #endregion

        #region ad connection
        public void adconn() {
            try {
                dentry = new directoryentry("ldap://mydomain", "uid", "pass", authenticationtypes.secure);
                ds = new directorysearcher(dentry);
            }
            catch (exception ex) {
                sw.write("error conecting active directory: " + ex.message);
                sw.close();
            }
        }
        #endregion ad connection

        #region test
        public directoryentry getdirectory() {
            directoryentry ode;
            ode = new directoryentry(dentry);
            return ode;
        }
        private directoryentry getallusers() {
            directoryentry de = getdirectory();
            directorysearcher desearch = new directorysearcher();
            desearch.searchroot = de;

            desearch.filter = "(&(objectclass=user))";
            desearch.searchscope = searchscope.subtree;
            searchresult results = desearch.findone();

            if (!(results == null)) {
                de = new directoryentry(results.path, "uid", "pass", authenticationtypes.secure);
                return de;
            }
            else {
                return null;
            }
        }
    #endregion test


        #region xcadsqlconnection
        public void sqlconn() {
            try {
                sqlconn = new sqlconnection(connstring);
                sqlconn.open();
                mycmd = new sqlcommand("select * view_join", sqlconn);
            } catch (exception ex) {
                sw.write("error conecting view: " + ex.message);
                sw.close();
            }
        }
        #endregion

        #region findemployee
        public bool findemp() {
            try {
                ds = new directorysearcher(dentry, query(), new string[] { "adspath" });
                searchresult sresult = ds.findone();
                if (sresult == null) {
                    sw.write("employee not found");
                }
                directoryentry ent = sresult.getdirectoryentry();
                properties(ent);
            }
            catch (exception ex) {
                sw.write("error looking employee in active directory: " + ex.message);
                sw.close();
                return false;
            }
            return false;
        }
        #endregion

        #region updateemployee
        public bool updateemp() {
            try {
                findemp();
                values(dentry);
                dentry.commitchanges();
            }
            catch (exception ex) {
                sw.write("error updating employee in active directory: " + ex.message);
                sw.close();
                return false;
            }
            return false;
        }
        #endregion

        #region query
        public string query(string attribute, string srttext) {
            try {
                if (srttext.length > 0) {
                    return "(" + attribute + "=" + srttext + ")";
                }
                return "";
            }
            catch (exception ex) {
                sw.write("error in query string value: " + ex.message);
                sw.close();
                sqlconn.close();
            }
            return "";
        }
        #endregion

        #region qryemployee
        public string query() {
            string s = "";
            string sfilter = "(&(objectcategory=person)(objectclass=user)";
            s += query("employeeid", employeeid);
            if (s.length > 0) {
                sfilter += s + ")";
            }
            else {
                sfilter += ")";
            }
            return sfilter;
        }
        #endregion

        #region writefile
        public void createlogfiles() {
            try {
                slogformat = datetime.now.toshortdatestring().tostring() + "" +
                datetime.now.tolongtimestring().tostring() + "==> ";

                string syear = datetime.now.year.tostring();
                string smonth = datetime.now.month.tostring();
                string sday = datetime.now.day.tostring();
                serrtime = syear + smonth + sday;
            }
            catch (exception ex) {
                sw.write("error: writing log file" + ex.message);
            }
        }
            public void errlog(string spathname, string serrmsg) {
            try {
                spathname = "@c:\\adupdt\\errlog\\errlog.txt";
                streamwriter sw = new streamwriter(spathname + serrtime, true);
                sw.writeline(slogformat + serrmsg);
            }
            catch (exception ex) {
                sw.write("cannot find file." + ex.message);
                sw.flush();
                sw.close();
            }
        }
        #endregion

        #region values
            private void values(directoryentry ent) {
                try {
                    ent.properties["telephonenumber"].value = telnum;
                    ent.properties["ipphone"].value = ipphone;
                    ent.properties["mobile"].value = mobile;
                    ent.properties["pager"].value = epager;
                    ent.properties["department"].value = dpt;
                    ent.properties["manager"].value = manager;
                }
                catch (exception ex) {
                    sw.write("error in values: " + ex.message);
                    sw.close();
                }
            }
            #endregion
        
        #region properties
            private void properties(directoryentry currentry) {
                if (currentry == null) {
                    return;
                }
                employeeid = currentry.properties["employeeid"].value.tostring();
                dentry = currentry;
            }

            public string employeeid {
                get { return employeeid; }
                set { employeeid = value; }
            }

            public string firstname {
                get { return fname; }
                set { fname = value; }
            }

            public string lastname {
                get { return lname; }
                set { lname = value; }
            }

            public string telephonenumber {
                get { return telnum; }
                set { telnum = value; }
            }

            public string ipphone {
                get { return ipphone; }
                set { ipphone = value; }
            }

            public string mobile {
                get { return mobile; }
                set { mobile = value; }
            }

            public string pager {
                get { return epager; }
                set { epager = value; }
            }

            public string manager {
                get { return manager; }
                set { manager = value; }
            }

            public string department {
                get { return dpt; }
                set { dpt = value; }
            }

            public string initials {
                get { return initials; }
                set { initials = value; }
            }

            public string displayname {
                get { return dispname; }
                set { dispname = value; }
            }

            public string email {
                get { return email; }
                set { email = value; }
            }

            public string homephone {
                get { return homephone; }
                set { homephone = value; }
            }

            public string title {
                get { return title; }
                set { title = value; }
            }

            public string filepath {
                get { return filepath; }
                set { filepath = value; }
            }
            #endregion properties

        #region webmethods
        #region getxcadfinemps
        [webmethod(description = "get employees")]
        public dataset getxemps() {
            sqlconnection xconn = new sqlconnection(connstring);
            xconn.open();
            dataset xds = new dataset();
            sqldataadapter xadapter = new sqldataadapter("select * viewjoin", xconn);
            xadapter.fill(xds, "view_join");
            return xds;
        }
        #endregion

        #region employees active directory
        [webmethod(description = "get employees active directory")]
        public dataset getademps() {
            sqlconnection aconn = new sqlconnection(connstring);
            dataset adds = new dataset();
            sqldataadapter adadapater = new sqldataadapter();
            adadapater.selectcommand = new sqlcommand("select * view_activedirectory", aconn);
            adadapater.fill(adds, "view_activedirectory");
            return adds;
        }
        #endregion
        #endregion webmethods
       

        private void initializecomponent() {

        }
    }
}

thanks.

although you've been given task create web service job, question not web services. looks like three questions:

  1. how data database necessary match ad
  2. how match ad
  3. how update ad once youve got match

none of these web service questions.

 

first, know information need in order match ad? if not, should go find out.

 

then, know how query database information need in order match? if not, should talk understands database find out query need. if don't know how use query ado.net, should ask in ado.net forum (once you've been given query).

 

separately, should write method given information needed match against ad, , match. test , make sure works.

 

separately, should write method updates ad given directory entry update , data update with. test that, , make sure works.

 

next, once you've got separate pieces working, put them method queries database data needed match, matches against ad, updates it.

 

finally, once you've done that, time call method (which work, right?) web service. when that point, if need web services, please come here , ask. right now, you're far away needing web services.



Archived Forums A-B  >  ASMX Web Services and XML Serialization



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'