【问题标题】:The type initializer for 'Microsoft.IdentityModel.Clients.ActiveDirectory.Authenticator' threw an exception'Microsoft.IdentityModel.Clients.ActiveDirectory.Authenticator' 的类型初始化程序引发异常
【发布时间】:2015-08-31 07:27:59
【问题描述】:

使用 OpenId Connect 中间件 (dnxcore50; 1.0.0-beta6-* coreclr x64) 处理 AAD SSO ...

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
    };
});

在OnAuthorizationCodeReceived,以这种方式获取 Graph API 的上下文...

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");

适用于 dnx451 1.0.0-beta4 clr x86,但不适用于 dnxcore50 1.0.0-beta6-* coreclr x64

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

ClientId、AppKey、Authority、notification.Code 和 BaseAddres 的值应该没问题……它们在两个运行时之间没有改变。依赖是...

"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-*"

我也在AzureAD/azure-activedirectory-library-for-dotnet 发帖,但那里的答案似乎很慢。如果在这里回答,我会在那里更新+关闭。

【问题讨论】:

标签: adal


【解决方案1】:

我也遇到这个问题,我的场景是我在库项目中引用了这个包,然后我在控制台应用程序中调用该方法,然后控制台应用程序会抛出这个异常。

查看控制台应用的debug文件夹,发现漏掉了

Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll

所以解决方法是我也在控制台应用程序中引用这个包。

【讨论】:

  • 你拯救了这一天!谢谢!
【解决方案2】:

原来使用 ADAL NuGet 包的示例项目错​​误地声称它可以与 Core CLR 项目一起使用。

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

在该团队使这个包对 dnxcore50 友好之前,我将尝试将我自己的 Core CLR 版本从他们的源代码滚动到我的项目中。

【讨论】:

    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2013-05-15
    • 2013-11-08
    • 2019-07-08
    相关资源
    最近更新 更多