Can Authenticate New-CsOnlineSession in PowerShell Command Prompt, but not .NET Core Application RRS feed

  • Question

  • I am using the below code to create a new-csonlinesession in order to add new PSTN gateway.I am trying to setup new session and try to create new pstn usage:

        Import-Module SkypeOnlineConnector
        $secpasswd = ConvertTo-SecureString '******' -AsPlainText -Force
        $userCredential = New-Object System.Management.Automation.PSCredential ('****@****.com', $secpasswd)
        $sfbSession = New-CsOnlineSession -Credential $userCredential
        Import-PSSession $sfbSession
        Set-CsOnlinePstnUsage -Identity global -Usage @{add="International"}

        public static void ExecuteSynchronously()
                    using (PowerShell PowerShellInstance = PowerShell.Create())
                        // use "AddScript" to add the contents of a script file to the end of the execution pipeline.
                        // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
                        // PowerShellInstance.AddScript("param($param1) $d = get-date; $s = 'test string value'; " +
                        //"$d; $s; $param1; get-service");
                        PowerShellInstance.AddScript("Set-ExecutionPolicy RemoteSigned");
                        PowerShellInstance.AddScript("Import-Module 'SkypeOnlineConnector'");
                        PowerShellInstance.AddScript("$secpasswd = ConvertTo-SecureString '*****' -AsPlainText -Force");
                        PowerShellInstance.AddScript("$userCredential = New-Object System.Management.Automation.PSCredential ('****@*****.com', $secpasswd)");
                        PowerShellInstance.AddScript("$sfbSession = New-CsOnlineSession -Credential $userCredential");
                        PowerShellInstance.AddScript("Import-PSSession $sfbSession");
                        PowerShellInstance.AddScript("Set-CsOnlinePstnUsage -Identity global -Usage @{add='International - 6'}");
                        // invoke execution on the pipeline (collecting output)
                        Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
                        // check the other output streams (for example, the error stream)
                        if (PowerShellInstance.Streams.Error.Count > 0)
                            // error records were written to the error stream.
                            // do something with the items found.
                        // loop through each output object item
                        foreach (PSObject outputItem in PSOutput)
                            // if null object was dumped to the pipeline during the script then a null
                            // object may be present here. check for null to prevent potential NRE.
                            if (outputItem != null)
                                //TODO: do something with the output item 
                                Console.WriteLine(outputItem.BaseObject.ToString() + "\n");

    Powershell code is working as expected.But getting error in c#.
    I am getting "System.Management.Automation" "Could not find WWW-Authenticate header in response" error ?
    Thursday, August 22, 2019 4:10 AM