【发布时间】:2018-08-01 01:39:53
【问题描述】:
我遇到了一个看起来有点像 this one 的问题,但我在一个稍微不同的上下文中遇到了这个问题,并且除了“尝试使用不同的身份验证”之外,原始问题没有令人满意的答案flow',我认为这对我不起作用。
我有一个托管在 Azure 中的 Web API,使用 Active Directory 不记名令牌进行保护。作为 VSTS 构建/发布流程的一部分,我希望能够对此进行一些自动化集成测试。
为此,我的第一步是构建一个使用 AD 原生应用程序的控制台应用程序,以便我可以使用非交互式身份验证流程,并且在上周结束时,代码似乎可以正常工作。相关代码为:
private async Task<string> GetToken(string username, string password)
{
string resourceId = <application ID of AD web API app>;
string clientId = <application ID of AD native app>;
string authority = "https://login.microsoftonline.com/<mytennant>.onmicrosoft.com";
AuthenticationContext authContext = new AuthenticationContext(authority);
UserCredential credential = new UserCredential(username, password);
var result = await authContext.AcquireTokenAsync(resourceId, clientId, credential);
return result.AccessToken;
}
当我周五下班时,这段代码正在运行。当我今天回来时,它提供了以下堆栈跟踪:
parsing_wstrust_response_failed: Parsing WS-Trust response failed
at Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.WsTrust.WsTrustResponse.CreateFromResponseDocument(XDocument responseDocument, WsTrustVersion version)
at Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.WsTrust.WsTrustRequest.<SendRequestAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows.AcquireTokenNonInteractiveHandler.<PreTokenRequestAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows.AcquireTokenHandlerBase.<RunAsync>d__57.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.<AcquireTokenCommonAsync>d__37.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContextIntegratedAuthExtensions.<AcquireTokenAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at GetToken.Program.<GetToken>d__5.MoveNext()
在本地运行时,我可以故障转移到您第一次需要使用的交互式登录,以授予以后以非交互方式登录的权限。为此,我将上述代码中对AcquireToken 的调用替换为:
AuthenticationResult result = null;
try
{
// the same as the old call to AcquiteTokneAsync
result = await authContext.AcquireTokenAsync(resourceId, clientId, credential);
}
catch (AdalException aex)
{
// this failover uses an interactive flow, so can't be used to run
// automated deployment integration tests
string replyUri = <one of the reply URIs in the native AD app>;
result = await authContext.AcquireTokenAsync(resourceId, clientId, new Uri(replyUri));
}
所以,我可以以交互方式获取令牌,并且获得的令牌有效(就像我上周使用非交互式流程获得的令牌一样)。但是这个错误阻止了我对已部署的 Web 应用程序运行自动化测试作为我的持续交付管道的一部分。有没有办法避免这种解析错误?
【问题讨论】:
-
我已尝试将 ADAL.Net nuget 包更新到最新版本。这并没有解决问题。
-
嗯,我已经有一段时间无法使用 ADAL.NET 进行非交互式登录了。在 API 集成测试中,我已针对令牌端点手动完成令牌获取。
标签: c# .net azure-active-directory adal