mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
Unauthorized (401) error when calling the admin API with version 2 Azure AAD tokens. #688
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @AchoArnold on GitHub (Apr 29, 2025).
Originally assigned to: @StefH on GitHub.
Describe the bug
401When it is configured to use version 2 of the AAD token. This happens because of the validation code heree7310fbc7b/src/WireMock.Net/Authentication/AzureADAuthenticationMatcher.cs (L61)The
issuwerfromhttps://login.microsoftonline.com/{tennantID}/.well-known/openid-configurationis given ashttps://sts.windows.net/{tennantID}/but when you decode a v2 AAD token using jwt.ms, the issuer is set ashttps://login.microsoftonline.com/{tennantID}/v2.0And the recommendation is that instead of comparing the entire string, you should only compare that the tennants match. The identity model even has a helper method for validating the issuwer36fb5f5556/tools/CrossPlatformValidator/CrossPlatformValidation/CrossPlatformValidation/RequestValidator.cs (L42)Expected behavior:
I can make wiremock admin API requests with with AAD version 2 tokens
Test to reproduce
/__admin/requestsand you'll get 401 instead of 200Other related info
Provide additional information if any.
@StefH commented on GitHub (Apr 30, 2025):
@AchoArnold
I did update some code, but was not able to test it. (https://github.com/WireMock-Net/WireMock.Net/pull/1288)
Can you test latest preview
1.8.0-ci-19960(https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)
@StefH commented on GitHub (May 2, 2025):
@AchoArnold
I did update some code, but was not able to test it. (https://github.com/WireMock-Net/WireMock.Net/pull/1288)
Can you test latest preview
1.8.0-ci-19960(https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)
@AchoArnold commented on GitHub (May 2, 2025):
Hello @StefH
Thanks for the fast response, I'll be able to test it out on Monday next week.
@AchoArnold commented on GitHub (May 2, 2025):
Hello @StefH
I installed the nuget source but I can't see this speicifc CI version
@AchoArnold commented on GitHub (May 2, 2025):
@StefH I downloaded the nupkg from here https://dev.azure.com/stef/WireMock.Net/_build/results?buildId=9960&view=artifacts&pathAsName=false&type=publishedArtifacts and ran the specific version.
I can confirm that the bug is now fixed I'm authenticating with 200
@StefH commented on GitHub (May 2, 2025):
That's good news.
Would you be so kind to share the postman request or collection (without any traceable credentials to your system). I cannot find my own test anymore.
And can you also share the C# code for this? (I also cannot find any example code anymore..)
@AchoArnold commented on GitHub (May 2, 2025):
Hello @StefH , I don't have any postman but I can send the instructions here.
To get v2.0 AAD token you need to modify the
Manifestof your AAD app registration by following the instructions here https://docs.azure.cn/en-us/entra/identity-platform/scenario-protected-web-api-app-registration#accepted-token-versionYou can then get the token using this CURL command
Once you obtain the token you can verify that it is version 2 using this website https://jwt.ms, I start the wiremock server. I'm using the
WireMock.Net.StandAlonepackage.And then I make a
GETrequest to{WiremockServerURL}/__admin/requestswith theBearer AadTokenset in theAuthorizationheader and 200 for success 401 for authentication errors.@AchoArnold commented on GitHub (May 2, 2025):
@StefH I just did a regression test and this new version of the wiremock.net package doesn't authenticate when I use a
ver1.0AAD token@StefH commented on GitHub (May 2, 2025):
@AchoArnold
I did a quick test for V1.
And the bearer token is decoded as:
And calling an /__admin endpoint in WireMock.Net works fine.
Can you double check how the issuer looks on your side?
@AchoArnold commented on GitHub (May 2, 2025):
@StefH I don't think the problem is the issuer but the issue is with
Audienceinv1theaudisapi://UUIDbut in v2 theaudisUUIDSo this can be fixed at the config level i.e
instead of
A few suggestions,
Thanks.
@StefH commented on GitHub (May 2, 2025):
1⃣
For C# code I do:
And I do a post to URL:
https://login.microsoftonline.com/<TENANT>/oauth2/tokenWith values:

And result is:
When I use this access_token in a GET request to
http://localhost:9091/__admin/settings(when running in debug step).All is fine.
(A NuGet version should be WireMock.1.8.1-ci-19970)
2⃣
Yes I checked that link, however, I thought I needed to include another NuGet for that logic, so I just build the logic myself.
3⃣
I think when you get the logging from wiremock, the exception should be present.
@AchoArnold commented on GitHub (May 5, 2025):
@StefH Any tips on how we can do this? I use the
WiremockConsoleLoggerand when there is an error this is the only log line which I seecfc13b2449/src/WireMock.Net/Owin/WireMockMiddleware.cs (L141)I don't see the results of the token validator to see if the problem is with the resource, issuer, or some other problem I had to reverse engineer the code to discover that the reason for authentication failures was because of issuer mismatch.Perhaps we can log
matchResult.Exception@StefH commented on GitHub (May 6, 2025):
@AchoArnold
Exception has been added to the logging.
Please test preview :
1.8.2-ci-19997@AchoArnold commented on GitHub (May 6, 2025):
Thanks @StefH Now the error is available in the logs.
@StefH commented on GitHub (May 6, 2025):
@AchoArnold
You need to use the correct audience in the setting I guess?
@AchoArnold commented on GitHub (May 6, 2025):
@StefH I was configured the
audiencewrongly on purpose to test that theExceptionlogging now works. And I can confirm that it works.Thanks.