【问题标题】:Get MFA informations in C# program在 C# 程序中获取 MFA 信息
【发布时间】:2019-08-01 20:36:03
【问题描述】:

我正在尝试在我的 C# 应用程序中获取有关 MFA 的信息。我已经在 Powershell 中取得了令人满意的结果,但我正在努力在 C# 中做同样的事情。

我在 Powershell 中的代码:

Get-MsolUser -SearchString c.test@mytenant.com | 
Where-Object {$_.StrongAuthenticationRequirements -like “*”} | 
Select-Object UserPrincipalName, DisplayName, @{n='MFA';e= 
{$_.StrongAuthenticationRequirements.State}}, @{n='Methods'; e= 
{($_.StrongAuthenticationMethods).MethodType}}, @{n='Default Method'; e= 
{($_.StrongAuthenticationMethods).IsDefault}}

UserPrincipalName : c.test@mytenant.com
DisplayName       : Cyril test
MFA               : Enforced
Methods           : {OneWaySMS, TwoWayVoiceMobile, PhoneAppOTP, 
PhoneAppNotification}
Default Method    : {False, False, False, True}

如您所见,我得到了 MFA 状态和使用的方法。

现在,我想在 C# 中做同样的事情。

我的功能:

public static List<string> GetMFA(Runspace runspace, string nom)
    {
        List<string> listResult = new List<string>();
        try
        {
            Command getLicenseCommand = new Command("Get-MsolUser");
            getLicenseCommand.Parameters.Add(new CommandParameter("SearchString", nom));
            var pipe = runspace.CreatePipeline();
            pipe.Commands.Add(getLicenseCommand);
            var props = new string[] { "displayname", "userprincipalname", "StrongAuthenticationRequirements" };
            Command CommandSelect = new Command("Select-Object");
            CommandSelect.Parameters.Add("Property", props);
            pipe.Commands.Add(CommandSelect);

            // Execute command and generate results and errors (if any).
            Collection<PSObject> results = pipe.Invoke();
            if (results.Count != 0)
            {
                var error = pipe.Error.ReadToEnd();
                if (error.Count > 0)
                {
                    throw new Exception(error[0].ToString());
                }

                foreach (PSObject resultat in results)
                {

                    string dn = resultat.Properties["displayname"].Value.ToString();
                    string upn = resultat.Properties["userprincipalname"].Value.ToString();
                    string mfa = resultat.Properties["StrongAuthenticationRequirements"].Value.ToString();

                    string res = dn + '/' + upn + '/' + mfa;
                    listResult.Add(res);
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

        return listResult;
    }

“StrongAuthenticationRequirements”属性没有返回“Enforced”之类的内容,而是

System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationRequirement]

我在这里错过了什么?

【问题讨论】:

  • 您是否费心探索列表及其对象?
  • @Seth 感谢您的回答。我没有设法遍历它似乎是一个列表。即使是调试模式下的 Visual Studio 工具也不能比这更进一步。无论如何,我设法通过直接查询 powershell 命令得到了一些结果,并且我得到了原始结果中的所有信息

标签: c# powershell


【解决方案1】:

我猜 OP 已经不在乎了,但是对于下一个像我在过去几个小时里遇到这个问题的人,我正在发布一个答案。

为了调用这些项目,您必须包含 Microsoft.Online.Administration 引用,这些引用作为 DLL 包含在 MSOL PowerShell 模块包中。

在 Visual Studio 中:

  1. 右键参考文献
  2. 选择“添加参考”
  3. 打开“浏览..”
  4. 导航到 MSOL 模块的安装位置。这可能会有所不同,具体取决于它是否安装在用户/计算机范围内。
  5. 我添加了 Microsoft.Online.Administration.PSModule

您现在可以在 c# 中解析结果。

注意:根据我的经验,我需要先将项目显式转换为变量,然后才能使用它们。


编辑:

经过进一步测试,我删除了对 resources.dll 的不必要引用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多