简体   繁体   中英

Login-AzureRmAccount command not working from hosted c# application

I am using powershell script to login into azure and for that i write simple command "Login-AzureRmAccount" and call that script into c# code on button click. It works fine locally but when i host this page on on server, authentication popup is not opening and i get error message ie "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"

Since your code is running on the server, the popup will try spawn on the local system (the server) and this is obviously not possible because you are not logged in to it.

The correct way to do this is using a Credential object as per the code posted at the end of this answer. You will need to create your own mechanism (form) for capturing the username and password.

Unfortunately, at the time of posting, Microsoft IDs do not support non-interactive flow (what you are trying to do). You will need to run your script on your client machine, login to your server, or set up an Azure AD Account with the appropriate credentials.

Using a Credential object to log in to Azure:

#Set up a PSCredential:
$yourPassword = ConvertTo-SecureString "<Your Password>" -AsPlainText –Force
$yourCredential = 
    New-Object -TypeName pscredential –ArgumentList "<Your UserName>", $yourPassword

# Then use that credential to log in to your Azure account 
Login-AzureRmAccount -Credential $yourCredential 
      -ServicePrincipal –TenantId <Your TenantId>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM