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

Starting Azure function with Auth produces error #16

Closed
klyse opened this issue Jul 12, 2022 · 3 comments
Closed

Starting Azure function with Auth produces error #16

klyse opened this issue Jul 12, 2022 · 3 comments

Comments

@klyse
Copy link

klyse commented Jul 12, 2022

	<PropertyGroup>
		<TargetFramework>net6.0</TargetFramework>
		<AzureFunctionsVersion>v4</AzureFunctionsVersion>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="AzureFunctions.Extensions.OpenIDConnect" Version="0.8.0" />
		<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
		<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.5.1" />
		<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
		<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="4.0.0-preview3" />
	</ItemGroup>

This is my csproj file containing this nuget: AzureFunctions.Extensions.OpenIDConnect.

Startup code:

	public override void Configure(IFunctionsHostBuilder builder)
	{
		var audience = builder.GetContext().Configuration.GetSection("OpenId:Audience").Get<string>();
		var issuer = builder.GetContext().Configuration.GetSection("OpenId:Issuer").Get<string>();
		var issuerUrl = builder.GetContext().Configuration.GetSection("OpenId:IssuerUrl").Get<string>();
		builder.Services.AddOpenIDConnect(config =>
		{
			config.SetTokenValidation(TokenValidationParametersHelpers.Default(audience, issuer));
			config.SetIssuerBaseUrlConfiguration(issuerUrl);
		});
	}

As soon as I start this it produces an error:

[2022-07-12T19:08:18.905Z] A host error has occurred during startup operation '86f8ac1e-833d-4af6-8eff-44100ae8deb0'.
[2022-07-12T19:08:18.905Z] AzureFunctions.Extensions.OpenIDConnect: Could not load file or assembly 'Microsoft.IdentityModel.Protocols.OpenIdConnect, Version=6.15.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
[2022-07-12T19:08:18.905Z] .
Value cannot be null. (Parameter 'provider')

I tried installing different versions of Microsoft.IdentityModel.Protocols.OpenIdConnect and Microsoft.IdentityModel.Protocols.WsFederation but that did not help.

@bryanknox
Copy link
Contributor

@klyse that problem might be caused by the Azure Functions (in-process) runtime removing assemblies that have the same names as assemblies used by the run-time. I think that can affect dependent assemblies too.

You might try adding the following to a <PropertyGroup> in your .csproj file.

<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>

That tells the Azure Functions runtime to preserve all assemblies used by the function app even if they have the same name as assemblies used in the run-time. _FunctionsSkipCleanOutput is undocumented by Microsoft but is sometimes recommended. For example, see: Azure/azure-functions-vs-build-sdk#397

An alternative to using _FunctionsSkipCleanOutput is to use FunctionsPreservedDependencies,
which is (minimally) documented, but not by Microsoft. It requires you to list the names of the specific assemblies
that you do not want to be removed if the name matches an assembly used in the Azure Functions
run-time. Hunting down the assembly names to list in FunctionsPreservedDependencies elements
is time consuming, and will have to be updated as the assemblies used by the functions run-time
or function app change.

There is a list of assemblies that the functions run-time uses. That list is maintained at:
https://github.com/Azure/azure-functions-host/blob/dev/tools/ExtensionsMetadataGenerator/test/ExtensionsMetadataGeneratorTests/ExistingRuntimeAssemblies.txt

See at:
bryanknox/AzureFunctionsOpenIDConnectAuthSample#27 (comment)

@bryanknox
Copy link
Contributor

I ended up writing a blog post about _FunctionsSkipCleanOutput and FunctionsPreservedDependencies, so I have a place to keep my notes for those
https://bryanknox.github.io/2022/07/15/functionsskipcleanoutput-and-functionspreserveddependencies.html

@klyse
Copy link
Author

klyse commented Jul 23, 2022

Just tried this. Works as expected. Thanks @bryanknox 😄

@klyse klyse closed this as completed Jul 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants