c# - 如何从同一 Azure AD 中的另一个 Web 应用程序访问 Web api?
问题描述
我在上面托管了一个 web ProductInfo
apiazure and register that application into Azure AD.
我有另一个web app UIProduct
,这个 Web 应用程序want to access the web api ProductInfo
都在同一个域和同一个 Azure AD 中。
我怎样才能访问 web ProductInfo
api web app UIProduct
?我需要再次生成任何令牌吗?
任何示例代码都会有所帮助。取自此链接的代码示例
成功登录后我在主页然后我点击关于我写这个的页面
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”
解决方案
您需要在Azure 门户中将这两个应用程序都注册为 Web 应用程序/API。
然后在您的 Web API 中,设置App ID URI
并创建您想要公开的任何范围。如果这是您将使用的唯一客户端,您可能只能拥有一个Access
范围,但请记住,这是最终用户在同意您的应用程序时将看到的内容。
在您的网络应用程序中,您将能够Required Permissions
为这个新的网络 API 和范围设置一个。这表明客户端应该需要对此 Web API 的同意,并且可以为其授予访问令牌。
此代码示例涵盖了这个确切的场景。
Web API 基础文档还涵盖了与此方案相关的一些概念信息
推荐阅读
- c# - 是否有一个列表来比较两个列表之间项目的属性?
- reactjs - 在 reactjs 中的 formitem 中添加验证
- java - 在 Macbook 中使用 Java 的 Eclipse 中,尝试在 Git 中提交我收到错误无法连接到任何存储库
- groovy - 在 CPI/HCI 中的 IFlow 中使用 groovy 脚本中的表达式交换属性保存初始有效负载
- nativescript - 在buttonclick nativescript上隐藏标签
- html - django中的散景图没有水平绘制
- c++ - 看似简单的循环的安全显式矢量化
- mysql - 如何通过 MySQL 中的查询从照片表中为每个帖子选择一张照片
- java - FileNotFoundException on method tomcat.addwebapp() 在fat jar中运行嵌入式tomcat,在maven的目标文件夹之外
- selenium - Selenium 无法选择元素,因为不存在直接选择器