我正在尝试为我公司运行 .net 4.8 的许多遗留 Web 表单应用程序之一实施 MSAL 身份验证。我已经安装了 Microsoft.Identity.Client 和相关的 nuget 包并创建了以下身份验证帮助程序类:
using Flurl.Http;
using Microsoft.Identity.Client;
using Newtonsoft.Json;
using System.Web;
using System.Configuration;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Threading.Tasks;
public static class Auth
{
public static async Task<string> Authenticate()
{
string clientId = ConfigurationManager.AppSettings["ClientId"];
string tenantId = ConfigurationManager.AppSettings["TenantId"];
string authority = "https://login.microsoftonline.com/" + tenantId;
IPublicClientApplication app = PublicClientApplicationBuilder.Create(clientId)
.WithRedirectUri("https://localhost:44374/")
.Build();
var scopes = new string[] {
"https://graph.microsoft.com/user.read"
};
var result = await app.AcquireTokenInteractive(scopes)
.ExecuteAsync();
string json = await "https://graph.microsoft.com/v1.0/me"
.WithOAuthBearerToken(result.AccessToken)
.GetStringAsync();
dynamic user = JsonConvert.DeserializeObject(json);
string displayName = user.displayName;
string userName = user.userPrincipalName;
HttpContext.Current.Session["displayname"] = displayName;
HttpContext.Current.Session["username"] = userName;
return json;
}
}
Webforms 应用程序构建没有问题,但是当调用 Auth.Authenticate() 方法时,出现以下异常:
Microsoft.IdentityModel.Abstractions 6.29.0 是由 nuget 安装的。收到异常后,我将此绑定重定向添加到 web.config 但它没有清除异常:
<dependentAssembly>
<assemblyIdentity name="Microsoft.IdentityModel.Abstractions" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.22.0.0" newVersion="6.22.0.0" />
</dependentAssembly>
你能指出我犯的错误吗?
提前致谢!
大卫
我也有同样的问题。我发现 Microsoft.Identity.Client 依赖于 Microsoft.IdentityModel.Abstractions (>= 6.22.0),但明确依赖于版本 6.22.0,而不是更高版本。同时,NuGet 正在安装最新版本的 Microsoft.IdentityModel.Abstractions,对我来说是版本 7.4.0。当在代码中调用 Identity.Client 时,dll 正在寻找对 IdentityModel.Abstractions 6.22.0 的引用,但找不到它并出错。
解决方案是在 NuGet 包管理器中安装 Microsoft.IdentityModel.Abstractions 版本 6.22.0。
希望微软能尽快解决这个问题。