Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Exception of type 'Microsoft.Online.Administration.Automation.MicrosoftOnlineException' was thrown.

$
0
0

Hello,

I have this error when i try to run my .net application who connect to O365 and get user information:

Exception of type 'Microsoft.Online.Administration.Automation.MicrosoftOnlineException' was thrown.

Here is my code :

public ActionResult testcao()
        {

            O365User user = new O365User();

            var secure = new SecureString();
            foreach (char c in password)
            {
                secure.AppendChar(c);
            }

            // Create Initial Session State for runspace.
            InitialSessionState initialSession = InitialSessionState.CreateDefault();
            initialSession.ImportPSModule(new[] { "MSOnline" });
            // Create credential object.
            PSCredential credential = new PSCredential(username, secure);
            // Create command to connect office 365.

            Command connectCommand = new Command("Connect-MsolService");
            connectCommand.Parameters.Add((new CommandParameter("Credential", credential)));

            Command GetMsolUserCommand = new Command("Get-MsolUser");
            GetMsolUserCommand.Parameters.Add(new CommandParameter("UserPrincipalName", "testcao@testcao.com"));

            using (Runspace psRunSpace = RunspaceFactory.CreateRunspace(initialSession))
            {
                // Open runspace.
                psRunSpace.Open();

                // Iterate through each command and executes it.
                foreach (var com in new Command[] { connectCommand, GetMsolUserCommand })
                {
                    var pipe = psRunSpace.CreatePipeline();
                    pipe.Commands.Add(com);
                    // Execute command and generate results and errors (if any).
                    Collection<PSObject> results = pipe.Invoke();
                    var error = pipe.Error.ReadToEnd();
                    if (error.Count > 0 && com == connectCommand)
                    {
                        ViewData["testcao"] = error[0].ToString();
                        return View();
                    }
                    else if (results.Count > 0 && com == GetMsolUserCommand)
                    {
                        // Fill user details on the form controls.
                        if (results.Count() <= 0)
                        {
                            ViewData["testcao"] = "No User";
                        }
                        else
                        {
                            PSObject psobject = results[0];
                            user.UserName = psobject.Properties["UserPrincipalName"].Value.ToString();
                            user.FirstName = psobject.Properties["FirstName"].Value.ToString();
                            user.LastName = psobject.Properties["LastName"].Value.ToString();
                            ViewData["testcao"] = user.UserName;
                        }
                    }
                }
                // Close the runspace.
                psRunSpace.Close();
                return View();
            }
        }

I have tried to run there powershell on windows powershell, it work well. But  on my application, it don't work.

Could you please help me to resolve it ?

Thanks in advanced,

Best regards,

Cao



Viewing all articles
Browse latest Browse all 21975

Trending Articles