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

  • Question

  • I am using the below code to create a new-csonlinesession in order to provision audio conference licensing:

    Get-PSSession | Remove-PSSession
    $userName = 'Chris@company.onmicrosoft.com'
    $passWord = Get-Content 'password.txt' | ConvertTo-SecureString
    $cred = New-Object System.Management.Automation.PsCredential($userName,$passWord)
    Write-host 'Connect to Skype/Teams Online' 
    Import-Module 'C:\Program Files\Common Files\Skype for Business Online\Modules\SkypeOnlineConnector\SkypeOnlineConnector.psd1'
    Import-Module 'C:\Program Files\Common Files\Skype for Business Online\Modules\LyncOnlineConnector\LyncOnlineConnector.psd1'
    $session = New-CsOnlineSession -Credential $cred -OverrideAdminDomain 'company.onmicrosoft.com'
    Import-PSSession $session -AllowClobber -Verbose

    I have no problems running this is in the powershell command prompt.

    Below is the code that I am using in my ASP.NET application to perform the same function:

     using (PowerShell ps = PowerShell.Create())
                        ps.AddScript("Set-ExecutionPolicy Unrestricted\n" +
                        "Get-PSSession | Remove-PSSession \n" +
                        "$userName = 'Chris@company.onmicrosoft.com'\n" +
                        $"$passWord = Get-Content 'password.txt' | ConvertTo-SecureString \n" +
                        "$cred = New-Object System.Management.Automation.PsCredential($userName,$passWord) \n" +
                        "Write-host 'Connect to Skype/Teams Online' \n" +
                        "Import-Module 'C:\\Program Files\\Common Files\\Skype for Business Online\\Modules\\SkypeOnlineConnector\\SkypeOnlineConnector.psd1' \n" +
                        "Import-Module 'C:\\Program Files\\Common Files\\Skype for Business Online\\Modules\\LyncOnlineConnector\\LyncOnlineConnector.psd1' \n" +
                        "$session = New-CsOnlineSession -Credential $cred -OverrideAdminDomain 'company.onmicrosoft.com'\n" +
                        "Import-PSSession $session -AllowClobber -Verbose \n" +
                        "Get-PSSession | Remove-PSSession \n");
                        Collection<PSObject> results = ps.Invoke();
                        foreach (var psObject in results)
                        foreach (ErrorRecord err in ps.Streams.Error)
                            Console.WriteLine("Non-terminating errors:");
                    catch (RuntimeException ex)
                        Console.WriteLine("Terminating error:");

    However, whenever I run this script, I get the following error:

    401 - Unauthorized: Access is denied due to invalid credentials. You do not have permission to view this directory or page using the credentials that you supplied. Could not find WWW-Authenticate header in response.

    Does anyone know how to resolve this issue? I also tried including 

    "Get-Service 'msoidsvc' \n" +
    "Start-Service 'msoidsvc' \n" +

    After importing the modules, but get this error instead:

    Service 'Microsoft Online Services Sign-in Assistant (msoidsvc)' cannot be started due to the following error: Cannot open msoidsvc service on computer '.'.


    • Moved by woshixiaobai Monday, June 17, 2019 3:06 AM move this case to the right forum
    Friday, June 14, 2019 3:17 PM

All replies

  • Hi CLS303,

    Thanks for visiting our forum. Then here is mainly focus on troubleshooting issues regarding SFB online. Based on your description, you have some issue with the SFB script, it is more related with SFB development area, so I'll move this thread to the Microsoft Unified Communications Web API forum to get a better response.
    Thanks for your understanding.

    Best Regards,
    Evan Jiang

    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, June 17, 2019 3:05 AM
  • Have you found any solution ?
    If yes please share with me,I am also facing same issue.Thanks 
    Wednesday, August 21, 2019 12:14 PM