asp.net - 以非交互方式向 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 的设置如下
我是否缺少某些东西可以帮助我。
解决方案
不确定为什么错误会显示“未知用户类型”,但我很确定这不起作用,因为您正尝试将 ROPC 流与个人 Microsoft 帐户一起使用。
资源所有者密码凭证 (ROPC) 流程在以下情况下不起作用:
- 用户是个人 Microsoft 帐户
- 用户启用了 MFA
- 用户密码已过期
如果我没记错的话,它也不适用于访客帐户。
一般来说,我建议避免使用这种身份验证方法,而是将用户重定向到 Azure AD 登录页面。无法使用多因素身份验证通常是避免此流程的一个重要原因。
我也很确定你serviceUri
错了。该参数应定义您希望为其提供令牌的 API,而不是 Azure AD 授权页面的 URL。
推荐阅读
- python - 美丽的汤错误地解析 XML
- swift - 如何在 WKWebView 中使用自动填充和钥匙串访问
- ios - 我如何从函数中调用检查值
- spring-webflux - 使用 RouterFunction 处理 WebFlux 中的错误
- android - 匕首自定义范围,如何?
- tree - Angular Material 6.0.1:如何扩展 Angular mat-tree 中的任何节点?
- git - 解决git中的合并冲突更改并提交后,它使用另一个用户作为作者
- javascript - 如何将数组放入数组
- c++ - 单个 cpp 文件的 Visual Studio 编译选项灰显
- java - 如何从 ProcessInstance 中检索执行