playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Question]: Pass NTLM authentication in API testing

Open boris-meerovich-sp opened this issue 4 years ago • 21 comments

Your question

Hi, How can I pass NTLM authentication in API testing? Now, I have only HttpCredentials to pass and I need NetworkCredential to be passed.

My API is behind the IIS Windows Authentication.

Please, advise.

boris-meerovich-sp avatar Oct 06 '22 11:10 boris-meerovich-sp

There is no built in support for it at the moment. However, you still should be able to respond to the security challenge manually using the APIRequestContext APIs. Leaving this open as a feature request to collect more feedback.

yury-s avatar Oct 06 '22 16:10 yury-s

It would be great to have it out of the box. I have just stumbled on it and as an ugly temporary solution used powershell step with curl where I can pass -UseDefaultCredentials directly.

monikandy avatar Oct 14 '22 06:10 monikandy

There is no built in support for it at the moment. However, you still should be able to respond to the security challenge manually using the APIRequestContext APIs. Leaving this open as a feature request to collect more feedback.

Thanks for the response. Any chance you have any examples about forwarding credentials via APIRequestContext?

boris-meerovich-sp avatar Oct 18 '22 05:10 boris-meerovich-sp

It would be great to have NTLM authentication support for HTTP proxy. In our case our Web application is having NTLM authentication hence all of our scenarios are blocked as of now. I would appreciate if we can prioritize NTLM authentication support for HTTP proxy, in upcoming releases.

mailtogagan avatar Oct 19 '22 04:10 mailtogagan

It would be great to have it out of the box. I have just stumbled on it and as an ugly temporary solution used powershell step with curl where I can pass -UseDefaultCredentials directly.

I am facing the same problem with NTLM Authentication. Please could you share more information about this workaround with Curl and PowerShell? I can get it working with RestSharp by passing -UserDefaultCredentials, but couldn't find a way to pass Curl in PlayWright APIRequest.

Abdulhasiib avatar Nov 25 '22 11:11 Abdulhasiib

It would be great to have it out of the box. I have just stumbled on it and as an ugly temporary solution used powershell step with curl where I can pass -UseDefaultCredentials directly.

I am facing the same problem with NTLM Authentication. Please could you share more information about this workaround with Curl and PowerShell? I can get it working with RestSharp by passing -UserDefaultCredentials, but couldn't find a way to pass Curl in PlayWright APIRequest.

I have a TS+Pw project, I have wrapped PS part in a method:

  private static async powershellCall(endpoint: string) {
    const {exec} = require('child_process');
    const lock = `curl -Method PUT -UseDefaultCredentials -Headers @{'Accept'='application/json';} http://${process.env.ENVIRONMENT}/api/${endpoint}`;
    exec(lock, {'shell': 'powershell.exe'}, (error, stdout, stderr) => {
    });
    return exec;
  }

monikandy avatar Nov 25 '22 14:11 monikandy

It would be great to have it out of the box. I have just stumbled on it and as an ugly temporary solution used powershell step with curl where I can pass -UseDefaultCredentials directly.

I am facing the same problem with NTLM Authentication. Please could you share more information about this workaround with Curl and PowerShell? I can get it working with RestSharp by passing -UserDefaultCredentials, but couldn't find a way to pass Curl in PlayWright APIRequest.

I have a TS+Pw project, I have wrapped PS part in a method:

  private static async powershellCall(endpoint: string) {
    const {exec} = require('child_process');
    const lock = `curl -Method PUT -UseDefaultCredentials -Headers @{'Accept'='application/json';} http://${process.env.ENVIRONMENT}/api/${endpoint}`;
    exec(lock, {'shell': 'powershell.exe'}, (error, stdout, stderr) => {
    });
    return exec;
  }

Thanks for sharing code snippet. That's helpful. I have one more question. How you're passing this method to the PlayWright APIRequest?

I got working curl command for my API call, but not sure how I link it to PlayWright APIRequest!

Is there a way to pass curl to PlayWright API call or need to continue using curl only for API testing.

Thanks again for your time and support.

Abdulhasiib avatar Nov 26 '22 05:11 Abdulhasiib

Thanks for sharing code snippet. That's helpful. I have one more question. How you're passing this method to the PlayWright APIRequest?

I am not passing it to Pw API Request, it is hidden in service and I just call it as a step in test, e.g. await commonElements.unlockElementViaApi(elementGuid); - so it is rather nasty hack then universal solution to a NTLM problem :)

monikandy avatar Nov 28 '22 07:11 monikandy

Any chance on this being implemented soon or is there perhaps an alternative way I can use NTLM auth when running the tests in TypeScript?

ChristopherVR avatar Jul 19 '23 01:07 ChristopherVR

We are facing a similar issue trying to test an api behind windows authentication NTLM

AtefBellaaj avatar Aug 23 '23 20:08 AtefBellaaj

Same challenges, Is there any update or expected release to handle this scenario

MahmoudOrban avatar Sep 04 '23 17:09 MahmoudOrban

@yury-s Apologies for tagging you directly. Any plans for getting this implemented in near future? Any suggestions on how to open Web Application using playwright with NTLM authentication.

mailtogagan avatar Nov 03 '23 01:11 mailtogagan

This would be very useful for my organization. NTLM is the default sign on method for us.

prlyons avatar Mar 14 '24 15:03 prlyons

Any updates on this?

ssnignite avatar Apr 02 '24 10:04 ssnignite

Anyone have any hack to over come this issue on playwright

Pls help me on this. thanks

jaikumarqa avatar May 20 '24 16:05 jaikumarqa

Using Selenium you can do this with Dev Tools and it works for NTLM.

var networkInterceptor = Driver.Manage().Network;
var handler = new NetworkAuthenticationHandler()
{
    UriMatcher = (url) => url.Host == "sitename",
    Credentials = new PasswordCredentials($"{Settings.AdUser}", Settings.AdPassword)
};
networkInterceptor.AddAuthenticationHandler(handler);
await networkInterceptor.StartMonitoring();
await Driver.Navigate().GoToUrlAsync("https://sitename/path");
var actualString = App.EventWrappedDriver.PageSource;
Assert.Contains(Settings.AdUser, actualString);
await networkInterceptor.StopMonitoring();

Is there a way to do this with .Net Playwright using Dev Tools as well?

Edit: I got this to work with NTML with the following code:

_browser = await _playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
    Channel = "chrome",
    Headless = false,
    Args = ["--start-maximized", "--incognito", "--auth-server-whitelist=\"_\"", "--auth-server-allowlist=\"*\""],
});
Context = await _browser.NewContextAsync(new BrowserNewContextOptions
{
    HttpCredentials = new HttpCredentials
    {

        Username = "user",
        Password = "password",
    },
    ViewportSize = ViewportSize.NoViewport,
});

JDCain avatar Nov 21 '24 19:11 JDCain

Any updates?

PavelGlebovAndritz avatar Jan 30 '25 14:01 PavelGlebovAndritz

Giving this a bump because I have a use case that would really benefit from being able to do NTLK authentication in Playwright.

ghost avatar Jun 05 '25 01:06 ghost

Same for me, I have a use case with NTLM authentication

dlecan avatar Jun 05 '25 15:06 dlecan

any update on this? I also have the same use case.

kumarjitu21 avatar Jun 13 '25 17:06 kumarjitu21