首页 > 解决方案 > 如何使用 c# 遍历文件和活动目录权限?

问题描述

我是一名程序员学生,我需要制作一个 Web 应用程序,允许查看所有文件夹、子文件夹和文件以及谁可以访问它们...我还需要列出活动目录的组和用户并遍历所有服务器以修改它们。但我是 ac# 菜鸟,我在 3 周前开始学习 asp.net ......我想知道是否有人可以通过解释所有 MVC 层之间的通信方式来帮助我。我已经编写了一些函数,但我不知道如何让它们交互...感谢您的帮助!(对不起我的英语不好,我是法国人...... ^^')

标签: c#asp.netsql-serverentity-frameworkactive-directory

解决方案


对于新手来说,您要问的问题确实很复杂。由于您刚开始并且正在使用 asp.net,我建议您从ASP.NET Core重新开始,它是构建在 .Net Core 之上的新 Web 框架。

那里有很多资源和课程。我建议你订阅 Pluralsight,有很多课程,而且真的很棒,几乎所有的老师都是 MVP。构建 ASP.NET Core 应用程序有一条路径(有点像职业生涯):.

我在 .net 核心中使用过一些活动目录组和用户,但您需要先了解最基本的知识。

以下代码包含我用来验证活动目录用户的一些方法,以及获取目录中所有计算机的列表,但正如我所说,您需要先了解基础知识才能做到这一点代码在您的应用程序中正常工作

public bool ValidateWithActiveDirectory(string domainUrl, string userName, string password) {
    using (var context = new PrincipalContext(ContextType.Domain, domainUrl)) {
        UserPrincipal UserPrincipal1 = new UserPrincipal(context);
        PrincipalSearcher search = new PrincipalSearcher(UserPrincipal1);
        foreach (UserPrincipal result in search.FindAll()) {
        }
        if (context.ValidateCredentials(userName, password)) {
            return true;
        }
    }
    return false;
}

public IEnumerable<Computer> GetActiveDirectoryComputers(string domainUrl) {
    IList<Computer> computers = new List<Computer>();
    PrincipalContext context = new PrincipalContext(ContextType.Domain, domainUrl);
    ComputerPrincipal computerPrincipal = new ComputerPrincipal(context);
    PrincipalSearcher search = new PrincipalSearcher(computerPrincipal);
    foreach (ComputerPrincipal result in search.FindAll()) {
        computers.Add(new Computer(result.Name));
    }
    return computers;
}

注意:如果您加入Visual Studio Dev Essentials,他们将为您提供 1 个月的免费复数视力订阅。


推荐阅读