Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Azure.Identity support using pfx file #25423

Closed
MarcoEnxuto opened this issue Nov 19, 2021 · 19 comments
Closed

[Question] Azure.Identity support using pfx file #25423

MarcoEnxuto opened this issue Nov 19, 2021 · 19 comments
Assignees
Labels
Azure.Identity Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that

Comments

@MarcoEnxuto
Copy link

MarcoEnxuto commented Nov 19, 2021

Query/Question
Hi,
According to this post, pfx files should be supported. Unfortunately i don't see any support, or at least i can't get it work.
I did follow the troubleshooting steps but i don't see what i'm doing wrong.
So, i posted here a question, since i'm using a local server with IIS and Environment variables.
The app throws an exception indicating Could not load the certificate....

Either way, the certificate is in a public path and accessible.

AZURE_CLIENT_CERTIFICATE_PATH C:\Users\Public\cert.pfx
AZURE_CLIENT_ID (guid)
AZURE_TENANT_ID (another guid)

Environment:

  • Azure.Identity 1.5.0, Microsoft Identity Client 4.37.0
  • Windows Server 2022 and .NET 5
  • Visual Studio 16.11.7
@ghost ghost added needs-triage This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Nov 19, 2021
@jsquire jsquire added Azure.Identity Client This issue points to a problem in the data-plane of the library. needs-team-attention This issue needs attention from Azure service team or SDK team labels Nov 19, 2021
@ghost ghost removed the needs-triage This is a new issue that needs to be triaged to the appropriate team. label Nov 19, 2021
@jsquire
Copy link
Member

jsquire commented Nov 19, 2021

Thank you for your feedback. Tagging and routing to the team members best able to assist.

@christothes
Copy link
Member

Hi @MarcoEnxuto - Does the following work if you try to construct the certificate directly using your file path? If not, what is the error?

new X509Certificate2(clientCertificatePath);

@christothes christothes added the needs-author-feedback More information is needed from author to address the issue. label Nov 19, 2021
@ghost ghost removed the needs-team-attention This issue needs attention from Azure service team or SDK team label Nov 19, 2021
@MarcoEnxuto
Copy link
Author

MarcoEnxuto commented Nov 19, 2021

Hi @christothes, no. I'm not instantiating that class.
I'm using the following members to initiate the connection to Azure service.

Here's a code snippet...
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAd")) .EnableTokenAcquisitionToCallDownstreamApi() .AddMicrosoftGraph(Configuration.GetSection("MicrosoftGraphAPI")) .AddInMemoryTokenCaches(); }

As soon i use the graph service client, it throws a "Could not load the certificate".
Can you confirm the AZURE_CLIENT_CERTIFICATE_PATH uses PFX file path? there's some incongruencies on help pages.

@ghost ghost added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels Nov 19, 2021
@christothes
Copy link
Member

Sorry, I know you aren't using that class directly, but it was intended as a test to see if Azure.Identity would be able to use your path since that is essentially what it does. Could you try this to verify that your pfx cert path is valid and that the password is unencrypted?

@christothes christothes added the needs-author-feedback More information is needed from author to address the issue. label Nov 19, 2021
@ghost ghost removed the needs-team-attention This issue needs attention from Azure service team or SDK team label Nov 19, 2021
@MarcoEnxuto
Copy link
Author

MarcoEnxuto commented Nov 19, 2021

Yes. I updated the question on Q&A, telling i checked that using Import-PfxCertificate Cmdlet on PowerShell. But i'll try here on C#. Good point.

@ghost ghost added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels Nov 19, 2021
@MarcoEnxuto
Copy link
Author

MarcoEnxuto commented Nov 19, 2021

@christothes after careful debug, that code raises The system cannot find the file specified.
This is a quite odd.
The file is present as you can see.
certificates
On the second image, the first line is the code you mentioned, with the path of the file above.
Am i doing anything wrong?
certificatedebug

@christothes
Copy link
Member

In the debugger immediate window, are you able to run these statements?

File.Exists(@"C:\Useres\Public\cert.pfx")
or
Directory.GetFiles(@"C:\Useres\Public");

@christothes christothes added the needs-author-feedback More information is needed from author to address the issue. label Nov 19, 2021
@ghost ghost removed the needs-team-attention This issue needs attention from Azure service team or SDK team label Nov 19, 2021
@MarcoEnxuto
Copy link
Author

Well, by using your suggestion, it exists...
But the class X509Certificate2 doesn't find the file, or even the env variable...

image

@ghost ghost added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels Nov 19, 2021
@christothes
Copy link
Member

This is very strange. Can you reproduce with X509Certificate2 directly?

@christothes christothes added the needs-author-feedback More information is needed from author to address the issue. label Nov 19, 2021
@ghost ghost removed the needs-team-attention This issue needs attention from Azure service team or SDK team label Nov 19, 2021
@MarcoEnxuto
Copy link
Author

MarcoEnxuto commented Nov 19, 2021

You bet it is @christothes.
But to clarify, I'm doing a remote debug from my dev box to the Server.
As you requested, just did that, and it throws as earlier.
image

Is it possible this happening because i have the debugger attached? I don't think so...

@ghost ghost added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels Nov 19, 2021
@MarcoEnxuto
Copy link
Author

Hi @christothes, did you reach any conclusion based on my findings? Thanks.

@MarcoEnxuto
Copy link
Author

Hello,
Any news regarding this @christothes?

@MarcoEnxuto
Copy link
Author

Hello, Any news regarding this @christothes?

Hi @schaabs, i guess @christothes might be Out of Office. Can you help me here?
Thanks a lot.

@christothes
Copy link
Member

Hi @MarcoEnxuto - Because this reproduces directly with X509Certificate2, this doesn't appear to be an issue with the client SDK. Have you seen this?

@christothes christothes added the needs-author-feedback More information is needed from author to address the issue. label Dec 6, 2021
@ghost ghost removed the needs-team-attention This issue needs attention from Azure service team or SDK team label Dec 6, 2021
@MarcoEnxuto
Copy link
Author

MarcoEnxuto commented Dec 7, 2021

Hi @MarcoEnxuto - Because this reproduces directly with X509Certificate2, this doesn't appear to be an issue with the client SDK. Have you seen this?

Hi @christothes, sorry to bother.
Well, i don't know what to say but i did read carefully your suggestion. What the link reports is an error when a user tries to instantiate X509Certificate2 class with a relative path. What i did, is the complete opposite. I use an absolute path when invoking the new instance. As you have seen earlier.

I tried another way by copying the pfx certificate file to the output directory where i deploy my web app, which was the initial intention of the author of that post. In the Immediate window i could execute successfully the code File.Exists (which returne true), but at runtime the code returns the exception "The system cannot find the file specified".

I also changed the AZURE_CLIENT_CERTIFICATE to a relative path, and i got the same outcome.
cert

Something is not right here, and it is interesting that i am the only one reporting this...

@ghost ghost added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels Dec 7, 2021
@christothes
Copy link
Member

I'm running out of ideas on this one, Perhaps you could verify the access control rules defined on the file or the containing directory?

Debug.WriteLine(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
var fileInfo = new FileInfo(@"<path to cert>");
var accessControl = fileInfo.GetAccessControl();
var rules = accessControl.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
foreach (FileSystemAccessRule rule in rules)
{
    Debug.WriteLine(rule.IdentityReference.Value);
}

@christothes christothes added the needs-author-feedback More information is needed from author to address the issue. label Dec 7, 2021
@ghost ghost removed the needs-team-attention This issue needs attention from Azure service team or SDK team label Dec 7, 2021
@MarcoEnxuto
Copy link
Author

Hi again @christothes,
The code snippet printed the below info:

IIS APPPOOL<websitename>

BUILTIN\IIS_IUSRS
BUILTIN\Administrators
NT AUTHORITY\SYSTEM
NT AUTHORITY\INTERACTIVE
NT AUTHORITY\SERVICE
NT AUTHORITY\BATCH

@ghost ghost added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels Dec 7, 2021
@christothes
Copy link
Member

Hi again @christothes, The code snippet printed the below info:

IIS APPPOOL

BUILTIN\IIS_IUSRS BUILTIN\Administrators NT AUTHORITY\SYSTEM NT AUTHORITY\INTERACTIVE NT AUTHORITY\SERVICE NT AUTHORITY\BATCH

Have you confirmed that those permissions allow IIS APPOOL to access the file?

The only other thought I had is to check if the certificate file is blocked. https://superuser.com/questions/590787/what-does-it-mean-when-a-file-is-blocked-in-windows

@christothes christothes added the needs-author-feedback More information is needed from author to address the issue. label Dec 7, 2021
@ghost ghost removed the needs-team-attention This issue needs attention from Azure service team or SDK team label Dec 7, 2021
@MarcoEnxuto
Copy link
Author

Hi @christothes, finally i found out what was wrong. And this took a while.
It seems we have to change on IIS Application Pool the following setting: Load user profile=true within the Process Model property Group. This link told me the solution. Another life saved.

Instead of using Web App service on Azure for development purposes, because well... costs... I decided to deploy on prem, using IIS.
So, for those who have the same thinking, and want to test the integration before deploying to Azure, on a Staging/Prod environments, you should consider the following configurations
First, get a certificate that was already configured on AAD, and place it on the local server.
Second, configure the Environment variables such as the image below
image

Third, enable the "Load User profile" in the Application Pool, click ok and recycle it.
Now the Azure.Identity loads the certificate properly.

Thank you very much @christothes for your patience on this subject and thanks for your suggestions, i did help on my research.

On a side note: i wish you Merry Christmas for you and your family.

@ghost ghost added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels Dec 8, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Azure.Identity Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
None yet
Development

No branches or pull requests

4 participants