首页 > 解决方案 > 以非交互方式向 Azure AD 进行身份验证

问题描述

我有一个 asp.net 应用程序,我试图在其中集成 AD 身份验证而不将用户重定向到https://login.microsoftonline.com。我使用以下代码并根据最新文档进行了一些修改,但出现如下错误

Microsoft.IdentityModel.Clients.ActiveDirectory.AdalException: 'unknown_user_type: Unknown User Type'

这是我写的代码

var tenant = System.Configuration.ConfigurationManager.AppSettings["Tenant"];
var serviceUri = "https://login.microsoftonline.com/4061611f-98d4-4484-944c-3796d4c9746f/oauth2/authorize";
var clientID = System.Configuration.ConfigurationManager.AppSettings["ClientId"];
var userName = "";
var password = "";
var authority = string.Format(System.Globalization.CultureInfo.InvariantCulture, System.Configuration.ConfigurationManager.AppSettings["Authority"], tenant);
var authContext = new AuthenticationContext(authority);
 var credentials = new UserPasswordCredential(userName, password);
 var authResult = await authContext.AcquireTokenAsync(serviceUri, clientID, credentials);

租户和应用程序 ID 的设置如下

在此处输入图像描述

在此处输入图像描述

我是否缺少某些东西可以帮助我。

标签: asp.netazure-active-directory

解决方案


不确定为什么错误会显示“未知用户类型”,但我很确定这不起作用,因为您正尝试将 ROPC 流与个人 Microsoft 帐户一起使用。

资源所有者密码凭证 (ROPC) 流程在以下情况下不起作用:

  • 用户是个人 Microsoft 帐户
  • 用户启用了 MFA
  • 用户密码已过期

如果我没记错的话,它也不适用于访客帐户。

一般来说,我建议避免使用这种身份验证方法,而是将用户重定向到 Azure AD 登录页面。无法使用多因素身份验证通常是避免此流程的一个重要原因。

我也很确定你serviceUri错了。该参数应定义您希望为其提供令牌的 API,而不是 Azure AD 授权页面的 URL。


推荐阅读