我无法使用.net核心控制台应用程序的用户名和密码登录我的Azure B2C Active Directory。我正在使用nuget的MSAL Library 2.6。我在GitHub上找到了这个例子https://github.com/Azure-Samples/active-directory-dotnetcore-console-up-v2
我把代码减少到最小,它看起来像这样:
public static async Task TestLogin()
{
try
{
var scopes = new[] { "community.member" };
var username = "{[email protected]}";
var password = new SecureString();
foreach (var c in "{Password}")
{
password.AppendChar(c);
}
var app = new PublicClientApplication("{B2C Application ID}", "https://{Application Name}.b2clogin.com/tfp/xxxxxxx-xxxx-xxxxxxxxxxxx/B2C_1_NativeSigneIn");
var result = await app.AcquireTokenByUsernamePasswordAsync(scopes, username, password);
Console.WriteLine($"Token: {result.AccessToken}");
}
catch (MsalServiceException ex)
{
Console.WriteLine($"{ex.Message} - {ex.ErrorCode}");
}
}
我总是得到以下MSAL异常:消息:用户域发现失败ErrorCode:user_realm_discovery_failed
旧库有一个错误。新版本的MSAL有修复。您可以在此处阅读有关问题和解决方案:
https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/926
基本上,您需要下载其中一个开发人员从GITHUB源编译的软件包的本地版本,通过将NuGet软件包管理器指向下载的软件包从您的解决方案中引用该软件包,然后您需要为新的Fluent API重新编写API对于MSAL,它会工作!很简单。