kandarp kandarp - 3 months ago 88
PowerShell Question

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 i.e. "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"

Answer

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 follows. You will need to create your own mechanism (form) for capturing the username and password.

#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>