比较来自ActiveDirectory的数据并将其存储到db中。

目前,我创建的ActiveDirectory用户被存储到以下地址 file 而且工作得很好。我现在想要的是,我需要将数据从 ActiveDirectory 和数据来自 database 如果状态为0,则改变它并存储到数据库中。这意味着,如果帐户是活跃的=1是不活跃的=0.到目前为止,我创建了这个

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.DirectoryServices;
using System.IO;

namespace ActiverDirectory
{
    public class Korisnik
    {
        public int Id { get; set; }
        public string Username { get; set; } //samaccountname
        public string DisplayName { get; set; } //displayname      
        public bool isEnabled { get; set; } //AccountExpires 
        public bool PassNevExp { get; set; }   //PassExpires 

    }

    class Program
    {
        static void Main(string[] args)
        {
            foreach (Korisnik korisnik in VratiKorisnike())
            {
                Console.WriteLine(korisnik);
            }

        }



        public static List<Korisnik> VratiKorisnike()
        {
            List<Korisnik> lstADUsers = new List<Korisnik>();
            string sDomainName = "saoos";
            string DomainPath = "LDAP://" + sDomainName;



            string constring = @"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True";
            string Query = "SELECT * FROM tblZaposleni_AD";
            DataTable table = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(Query, constring);
            adapter.Fill(table);

            string txt = "";
            string fileLoc = @"C:\output.txt";

            foreach (DataRow row in table.Rows)
            {
                string line = "";
                foreach (DataColumn column in table.Columns)
                {
                    line += "," + row[column.ColumnName].ToString();
                }
                txt += line.Substring(1);
            }
            using (var sw = new StreamWriter(fileLoc))
            {
                sw.WriteLine(txt);
            }

            Console.WriteLine("Ok");

            DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
            DirectorySearcher search = new DirectorySearcher(searchRoot);


            search.Filter = "(&(objectClass=user)(objectCategory=person))";

            search.PropertiesToLoad.Add("samaccountname"); // Username
            search.PropertiesToLoad.Add("displayname"); // display name
            search.PropertiesToLoad.Add("userAccountControl");  // isEnabled
            search.PropertiesToLoad.Add("pwdLastSet"); //passwordExpires



            DataTable resultsTable = new DataTable();
            resultsTable.Columns.Add("samaccountname");
            resultsTable.Columns.Add("displayname");
            resultsTable.Columns.Add("Neaktivan");
            resultsTable.Columns.Add("dontexpirepassword");



            SearchResult result;

            SearchResultCollection resultCol = search.FindAll();

            if (resultCol != null)
            {
                for (int counter = 0; counter < resultCol.Count; counter++)
                {
                    string UserNameEmailString = string.Empty;

                    result = resultCol[counter];

                    if (result.Properties.Contains("samaccountname")
                        && result.Properties.Contains("displayname"))
                    {
                        int userAccountControl = Convert.ToInt32(result.Properties["userAccountControl"][0]);
                        string samAccountName = Convert.ToString(result.Properties["samAccountName"][0]);

                        int isEnable;
                        int Dont_Expire_Password;

                        if (userAccountControl > 0)
                        {
                            isEnable = 0;
                        }
                        else
                        {
                            isEnable = 1;
                        }


                        if ((userAccountControl & 65536) != 0)
                        {
                            Dont_Expire_Password = 1;
                        }
                        else
                        {
                            Dont_Expire_Password = 0;
                        }

                        Korisnik korisnik = new Korisnik();
                        korisnik.Username = (result.Properties["samaccountname"][0]).ToString();
                        korisnik.DisplayName = result.Properties["displayname"][0].ToString();
                        korisnik.isEnabled = Convert.ToBoolean(result.Properties["userAccountControl"][0]);

                        //long value = (long)result.Properties["pwdLastSet"][0];
                        //DateTime pwdLastSet = DateTime.FromFileTimeUtc(value);

                        DataRow dr = resultsTable.NewRow();
                        dr["samaccountname"] = korisnik.Username.ToString();
                        dr["displayname"] = korisnik.DisplayName.ToString();
                        dr["neaktivan"] = Math.Abs(isEnable);
                        dr["dontexpirepassword"] = Dont_Expire_Password;


                        resultsTable.Rows.Add(dr);
                        lstADUsers.Add(korisnik);
                    }
                }
                var json = JsonConvert.SerializeObject(resultCol, Formatting.Indented);
                var res = json;
                Console.WriteLine("Ispis uspjesno obavljen");
                Console.ReadLine();
                File.WriteAllText(fileLoc, json);
            }
            return lstADUsers;
        }
    }
}

到目前为止,所有的东西都在这里,我只需要把这个存储到。[db].[table]这是一些和平的代码,它可以从ActiveDirectory中检查UserAccount是否是。activedisable如果账户是活动的 ActiveDirectory 并在 [db].[table] 处于非活动状态,将状态改为1并存储数据。当我运行这个时,我得到了正确的输出,但状态没有改变。任何想法

解决方案:

试试这样的东西。

    public class Korisnik
    {
        public int Id { get; set; }
        public string Username { get; set; } //samaccountname
        public string DisplayName { get; set; } //displayname      
        private bool _isEnabled { get; set; } //AccountExpires 
        public int isEnabled {
            get { return (_isEnabled == true)? 1 : 0;}
            set { _isEnabled = (value == 0) ? false : true; }
        } //AccountExpires 
        public bool PassNevExp { get; set; }   //PassExpires 

    }

给TA打赏
共{{data.count}}人
人已打赏
未分类

Datepicker bootstrap 4问题巫师改变了

2022-9-9 3:29:22

未分类

PHP 接下来的Y分钟内,每X分钟的日期和时间是什么?

2022-9-9 3:40:18

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索