The type initializer for 'Microsoft.IdentityModel.Clients.ActiveDirectory.Authenticator' threw an exception

5.5k views Asked by At

Working with AAD SSO using OpenId Connect middleware (dnxcore50; 1.0.0-beta6-* coreclr x64) ...

app.UseCookieAuthentication(options => { options.AutomaticAuthentication = true; });
app.UseOpenIdConnectAuthentication(options => {
    options.ClientId = ClientId;
    options.Authority = Authority;
    options.PostLogoutRedirectUri = PostLogoutRedirectUri;
    options.AutomaticAuthentication = true;
    options.SecurityTokenValidators = new[] { new UnsafeJwtSecurityTokenHandler() };
    options.Notifications = new OpenIdConnectAuthenticationNotifications {
        AuthenticationFailed = OnAuthenticationFailed,
        AuthorizationCodeReceived = OnAuthorizationCodeReceived
    };
});

In OnAuthorizationCodeReceived, getting a context for Graph API this way ...

string userObjectId = notification.AuthenticationTicket.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
ClientCredential credential = new ClientCredential(ClientId, AppKey);
AuthenticationContext authContext = new AuthenticationContext(Authority);
AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(notification.Code, new Uri(BaseAddress), credential, "https://graph.windows.net");

works with dnx451 1.0.0-beta4 clr x86 but fails for dnxcore50 1.0.0-beta6-* coreclr x64 with

The type initializer for 'Microsoft.IdentityModel.Clients.ActiveDirectory.Authenticator' threw an exception.

Values for ClientId, AppKey, Authority, notification.Code, and BaseAddres should be ok ... they weren't changed between the two runtimes. Dependencies are ...

"Microsoft.IdentityModel.Protocol.Extensions": "2.0.0-*",
"Microsoft.AspNet.Mvc": "6.0.0-*",
"Microsoft.AspNet.Authentication.Cookies": "1.0.0-*",
"Microsoft.AspNet.Diagnostics": "1.0.0-*",
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
"Microsoft.AspNet.StaticFiles": "1.0.0-*",
"Microsoft.AspNet.Tooling.Razor": "1.0.0-*",
"Microsoft.IdentityModel.Clients.ActiveDirectory": "3.1.203031538-alpha",
"Microsoft.Framework.ConfigurationModel": "1.0.0-*",
"Microsoft.AspNet.Authentication.OpenIdConnect": "1.0.0-*",
"Microsoft.AspNet.Identity": "3.0.0-*",
"Microsoft.AspNet.Http": "1.0.0-*"

I also posted at AzureAD/azure-activedirectory-library-for-dotnet, but answers seem to arrive slowly there. I'll update+close over there if answered here.

2

There are 2 answers

0
GuardRex Security On

Turns out that the sample project using the ADAL NuGet package incorrectly claims that it can be used with a Core CLR project.

https://github.com/aspnet/Home/issues/674#issuecomment-112683593

Until that team makes this package dnxcore50-friendly, I'm going to attempt to roll my own Core CLR version from their source into my project.

1
andycnzh On

I encountered this problem too, my scenario is I reference this package in library project, then I invoke the method in console application, then the console application will throw this exception.

Check the debug folder of console application, I found it missed

Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll

So the workaround is that I reference this package in console application too.