restactive-directorysingle-sign-onpowershell-4.0

Execute a RESTAPI request from Powershell using the credentials of the user logged into the windows machine(AD Credential's)


I want to login/communicate to a remote system using RESTAPI and I want to use the same AD credentials of the user firing the script from a windows machine. Example:- I am logged into a Windows server with the credentials "Domain\User" with password "password" and want to login using the same credentials to login to a remote system without entering the username and password.


Solution

  • Try using WebClient class:

    $webClient = new-object System.Net.WebClient
    $webClient.UseDefaultCredentials = $true
    $reply = $webClient.DownloadString("http://google.com")
    $reply
    

    The main thing here is to set UseDefaultCredentials property of WebClient to true. Method DownloadString is just an example of how to use WebClient. There are plenty of other methods which you can use depending on your REST endpoint. You can find full list here.