【发布时间】:2019-03-14 14:56:17
【问题描述】:
我在 azure and register that application into Azure AD. 上托管了一个网络 API ProductInfo
我还有另一个 web app UIProduct 和这个 Web 应用程序 want to access the web api ProductInfo 都在同一个域和同一个 Azure AD 中。
如何从 web app UIProduct 访问 web api ProductInfo ?
我需要再次生成任何令牌吗?
任何示例代码都会有所帮助。 取自 link 的代码示例
成功登录后,我在主页,然后我点击关于我写这篇文章的页面
public async System.Threading.Tasks.Task<ActionResult> About()
{
AuthenticationResult result = null;
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID));
ClientCredential credential = new ClientCredential(clientId, clientSecret);
result = await authContext.AcquireTokenSilentAsync("App ID URI", credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://demotest.azurewebsites.net/api/getdata");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpResponseMessage response = await client.SendAsync(request);
出现异常 - “无法以静默方式获取令牌,因为没有令牌 在缓存中找到。调用方法 AcquireToken"
【问题讨论】:
-
我已尽我所能回答,但我建议您清理问题的格式并提供有关您尝试构建的体验的更多信息。有点不清楚究竟是什么让你感到困惑,如果我的答案遗漏了什么,请告诉我!
-
您希望一个广告应用访问另一个广告应用?
-
是的!我尝试了此代码示例,但未能为 webapi 生成令牌,但无法生成令牌 azure.microsoft.com/en-in/resources/samples/…
标签: c# azure asp.net-web-api azure-active-directory