如何通过交互式提供凭据从 asp.net Web 表单应用程序连接到 Azure Active Directory?
How to connect to Azure Active Directory from asp.net web forms application by providing credentials interactively?
我想为任何用户提供通过我的网站连接到 powershell Azure Active Directory 模块并在其 Microsoft 租户中执行一些管理操作的能力。我使用的代码是:
InitialSessionState iss = InitialSessionState.CreateDefault2();
iss.ImportPSModule(new[] { "MSOnline" });
var shell = PowerShell.Create(iss);
var connectCmd = new Command("Connect-MsolService");
shell.Commands.AddCommand(connectCmd);
var results = shell.Invoke();
但我在 Invoke
之后收到以下错误:
Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application
当我在 PowerShell ISE 中 运行 PowerShell 代码时,情况并非如此:
Import-Module MSOnline
Connect-MsolService
相反,我得到了一个登录对话框,我可以在其中提供登录凭据。
我希望网站用户获得相同的登录对话框体验。
在 C# 代码中,我不想将凭据作为参数传递给 Connect-MsolService
cmdlet,而是以交互方式从用户那里收集它,以便它也适用于启用 MFA 的帐户。
注意:用户将与部署在网络服务器上的网站进行交互,而不会直接登录到服务器。嵌入在 C# 中的 Powershell 代码也将 运行 在 Web 服务器上。
到目前为止,我的互联网搜索还没有找到我可以使用的东西。有没有办法让它工作?请分享任何提示、想法和解决方案。
您的代码 运行 在 Web 服务器上(作为 Web 应用程序)而不是在使用它的客户端上 - 将尝试在服务器上打开弹出窗口 - 因此出现错误 !
因为您提到您将无法通过凭据。
您可以使用访问令牌进行身份验证的另一个选项。
Connect-Msolservice -MsGraphAcessToken <Token>
您使用的是C#,因此您可以利用MSAL(Microsoft 身份验证库)执行交互式登录并获取令牌。您可以参考this article了解更多信息。
获取的令牌随后可以传递给 powershell commandlet 以执行必要的操作。
我想为任何用户提供通过我的网站连接到 powershell Azure Active Directory 模块并在其 Microsoft 租户中执行一些管理操作的能力。我使用的代码是:
InitialSessionState iss = InitialSessionState.CreateDefault2();
iss.ImportPSModule(new[] { "MSOnline" });
var shell = PowerShell.Create(iss);
var connectCmd = new Command("Connect-MsolService");
shell.Commands.AddCommand(connectCmd);
var results = shell.Invoke();
但我在 Invoke
之后收到以下错误:
Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application
当我在 PowerShell ISE 中 运行 PowerShell 代码时,情况并非如此:
Import-Module MSOnline
Connect-MsolService
相反,我得到了一个登录对话框,我可以在其中提供登录凭据。
我希望网站用户获得相同的登录对话框体验。
在 C# 代码中,我不想将凭据作为参数传递给 Connect-MsolService
cmdlet,而是以交互方式从用户那里收集它,以便它也适用于启用 MFA 的帐户。
注意:用户将与部署在网络服务器上的网站进行交互,而不会直接登录到服务器。嵌入在 C# 中的 Powershell 代码也将 运行 在 Web 服务器上。
到目前为止,我的互联网搜索还没有找到我可以使用的东西。有没有办法让它工作?请分享任何提示、想法和解决方案。
您的代码 运行 在 Web 服务器上(作为 Web 应用程序)而不是在使用它的客户端上 - 将尝试在服务器上打开弹出窗口 - 因此出现错误 !
因为您提到您将无法通过凭据。
您可以使用访问令牌进行身份验证的另一个选项。
Connect-Msolservice -MsGraphAcessToken <Token>
您使用的是C#,因此您可以利用MSAL(Microsoft 身份验证库)执行交互式登录并获取令牌。您可以参考this article了解更多信息。
获取的令牌随后可以传递给 powershell commandlet 以执行必要的操作。