【发布时间】:2014-04-29 00:13:14
【问题描述】:
我正在实现以下场景:ASP.NET MVC 5 应用程序使用 Azure Active Directory 身份验证访问 OData WebAPI(如本文中:http://msdn.microsoft.com/en-us/magazine/dn463788.aspx)。
但是,当我调用 AuthenticationContext.AcquireToken 时,我得到 System.Threading.ThreadStateException 说:ActiveX 控件 '8856f961-340a-11d0-a96b-00c04fd705a2' 无法实例化,因为当前线程不在单线程中 公寓。
已编辑:
重现步骤:
使用组织身份验证创建新的 MVC 项目。使用您的 Windows Azure 域和 MSDN 帐户
通过 NuGet 添加 Actice 目录身份验证库
-
使用以下代码添加操作:
public async Task<ActionResult> Index(){ AuthenticationContext ac = new AuthenticationContext("https://login.windows.net/domain.onmicrosoft.com"); AuthenticationResult ar = ac.AcquireToken("https://domain.onmicrosoft.com/WindowsAzureADWebAPITest", "a4836f83-0f69-48ed-aa2b-88d0aed69652", new Uri("https://domain.onmicrosoft.com/myWebAPItestclient") ); // Call Web API string authHeader = ar.CreateAuthorizationHeader(); HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://server.com:44353/api/Values"); request.Headers.TryAddWithoutValidation("Authorization", authHeader); HttpResponseMessage response = await client.SendAsync(request); string responseString = await response.Content.ReadAsStringAsync(); return View(); } 运行代码并重现问题(AcqureToken 方法调用)。
请提出修复建议。
谢谢!
【问题讨论】:
-
"like in this article",如果文章没有重现问题,请简化发布代码。
标签: asp.net-mvc multithreading azure active-directory sta