【发布时间】:2021-12-24 09:12:57
【问题描述】:
我使用以下 C# 代码运行 PowerShell 脚本以连接到 Azure AD。 PS 脚本在 PS ISE 中运行良好。 C# 代码在控制台应用程序中运行良好。详细输出显示与帐户、环境、租户 ID 相关信息的成功连接
当相同的 C# 代码作为代码在我的 Web 应用程序中运行时,powershell.Invoke() 步骤需要 30 秒才能返回并且无法连接到 AzureAD。错误如下所示。
我已验证导入 AzureAD 模块、创建 $UserName、$Password 和 $Credential 变量的步骤是否有效。
private void ConnectAzureAD()
{
string PSModuleName = "C:\\PowerShellModules\\AzureAD";
string loginName = "myID@mydomain.com";
string loginPwd = "myPassword";
PowerShell powershell = PowerShell.Create();
try
{
powershell.AddScript("Import-Module -Name '" + PSModuleName + "' -Force");
powershell.AddScript("$UserName = '" + loginName + "'");
powershell.AddScript("$Password = ConvertTo-SecureString '" + loginPwd + "'-AsPlainText -Force -Verbose");
powershell.AddScript("$Credential = New-Object System.Management.Automation.PSCredential $UserName, $Password -Verbose");
powershell.AddScript("Connect-AzureAD -Credential $Credential -Verbose");
Collection<PSObject> PSOutput = powershell.Invoke();
}
catch (Exception ex)
{
}
}
Powershell.Streams.Verbose:
{Performing the operation "Connect-AzureAD" on target "Establishing a PowerShell session connected to {0} environment.".}
InvocationInfo: Command = {Connect-AzureAD}
Message: "Performing the operation \"Connect-AzureAD\" on target \"Establishing a PowerShell session connected to {0} environment.\"."
PipelineIterationInfo: Count = 2
Powershell.Streams.Error:
{One or more errors occurred.: Object reference not set to an instance of an object.}
CategoryInfo: {AuthenticationError: (:) [Connect-AzureAD], AadAuthenticationFailedException}
ErrorDetails: null
Exception: {"One or more errors occurred.: Object reference not set to an instance of an object."}
FullyQualifiedErrorId: "Connect-AzureAD,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD"
InvocationInfo: Command = {Connect-AzureAD}
PipelineIterationInfo: Count = 2
ScriptStackTrace: "at <ScriptBlock>, <No file>: line 1"
TargetObject: null
{One or more errors occurred.}
CategoryInfo: {AuthenticationError: (:) [Connect-AzureAD], AggregateException}
ErrorDetails: null
Exception: Count = 1
FullyQualifiedErrorId: "Connect-AzureAD,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD"
InvocationInfo: Command = {Connect-AzureAD}
PipelineIterationInfo: Count = 2
ScriptStackTrace: "at <ScriptBlock>, <No file>: line 1"
TargetObject: null
{Object reference not set to an instance of an object.}
CategoryInfo: {AuthenticationError: (:) [Connect-AzureAD], NullReferenceException}
ErrorDetails: null
Exception: {"Object reference not set to an instance of an object."}
FullyQualifiedErrorId: "Connect-AzureAD,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD"
InvocationInfo: Command = {Connect-AzureAD}
PipelineIterationInfo: Count = 2
ScriptStackTrace: "at <ScriptBlock>, <No file>: line 1"
TargetObject: null
{One or more errors occurred.: Object reference not set to an instance of an object.}
CategoryInfo: {NotSpecified: (:) [Connect-AzureAD], AadAuthenticationFailedException}
ErrorDetails: null
Exception: {"One or more errors occurred.: Object reference not set to an instance of an object."}
FullyQualifiedErrorId: "Microsoft.Open.Azure.AD.CommonLibrary.AadAuthenticationFailedException,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD"
InvocationInfo: Command = {Connect-AzureAD}
PipelineIterationInfo: Count = 0
ScriptStackTrace: "at <ScriptBlock>, <No file>: line 1"
TargetObject: null
【问题讨论】:
-
您能否在 connect-AzureAd 命令中调用 $Credential.password.MakeReadOnly() 之前尝试调用它,然后在凭据之前和 connect-AzureAd 之后检查 $Credential.Password.Length。
标签: c# powershell azure-active-directory