我创建了一个运行良好的多租户 Web API。现在我想构建一个本地客户端进行测试。Web API 应用程序在一个租户中定义。测试应用程序是在另一个租户中定义的,该租户已授予管理员对 Web API 的同意。
我想在我的(非交互式)集成测试中使用本机应用程序使用用户名和密码进行身份验证。我不能使用仅证书/应用程序身份验证,因为我需要真实的用户上下文。
获取令牌
var userCredential = new UserCredential("admin@clienttenant.onmicrosoft.com", "password");
var context = new AuthenticationContext("https://login.windows.net/common");
return context.AcquireToken("https://webapitenant.onmicrosoft.com/webApiResourceUri", testClientId, userCredential).AccessToken;
这段代码的问题在于,在我获得管理员同意本机应用程序之前它不起作用,即使用户与应用程序注册在同一个租户中。
抛出异常:
'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException' in Microsoft.IdentityModel.Clients.ActiveDirectory.dll
Additional information:
AADSTS65001: The user or administrator has not consented to use the application with ID 'nativeclientid'. Send an interactive authorization request for this user and resource.
由于测试不是交互式的,我必须创建一个使用上述代码但带有PromptBehaviour.Always
. 这将提示我输入用户名和密码并显示同意书。在我同意后,使用相同本机应用程序注册的测试开始工作。
有没有办法在没有交互式 GUI 的情况下接受同意书?