c# - 如何使用 Azure Mgmt SDK Fluent 获取端点统计信息和危险端点列表

标签 c# azure .net-core azure-management-api

我正在使用https://www.nuget.org/packages/Microsoft.Azure.Management.Fluent用于通过编程方式(C#.NET-Core Web 应用程序)获取 Azure 中的资源,并尝试通过提供服务主体(CS)来获取资源信息,如下所示...

 string subscriptionId = "xxx";
            string clientId = "xxx";
            string tenantId = "xxx";
            string clientSecret = "xxx";

            AzureCredentials cred = new AzureCredentialsFactory()
                .FromServicePrincipal(
                clientId,
                clientSecret,
                tenantId,
                AzureEnvironment.AzureGlobalCloud
                );

            var azure = Azure.Configure()
                             .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                             .Authenticate(cred)
                             .WithSubscription(subscriptionId);

任何示例代码(C#.NET-Core Web 应用程序),用于查找端点统计信息(循环遍历 NSG 中的开放端口并详细列出它们)和危险端点 (循环遍历 NSG 中的开放端口并识别 3389/22 等端口)。

请提供以上建议。

谢谢

最佳答案

如果您的意思是列出 NSG -> 入站安全规则中的所有端口,如下面的屏幕截图:

enter image description here

然后您可以使用如下代码:

        foreach (var nsg in azure.NetworkSecurityGroups.List())
        {
            
            var rules = nsg.SecurityRules;

            foreach (var r in rules)
            {
                Console.WriteLine($"*** the NSG: {r.Value.Name} ***");

                if (r.Value.DestinationPortRange != null)
                {
                    //after you get the port, you can apply your logic here.
                    Console.WriteLine(r.Value.DestinationPortRange);
                }

                if (r.Value.DestinationPortRanges != null)
                {
                    foreach (var port in r.Value.DestinationPortRanges)
                    {
                        //after you get the port, you can apply your logic here.
                        Console.WriteLine(port);
                    }
                }
                Console.WriteLine("**end**");
            }
          }

关于c# - 如何使用 Azure Mgmt SDK Fluent 获取端点统计信息和危险端点列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63259631/

相关文章:

java - 无法使用java代码从azure服务器下载图像

azure - 使用图形 api 对 Azure AD 中的 guest 用户进行身份验证

c# - 我如何获得总堆分配? GC.GetAllocatedBytesForCurrentThread() 不这样做

.net - 为 .NET Core/UWP 中的单个 HTTP 请求设置自定义 WebProxy

c# - IronScheme 无法完成哪些要求?

javascript - 选择选项时jquery提交选择表单

azure - 如何使用 Azure Functions 检查授权

c# - EF-core OnModelCreating 方法中的依赖注入(inject)

c# - 如何使用 html 按钮在 asp.net 中注销?

c# - 在代码中初始化用户控件不起作用 - MVVM WPF