概述我从以前的post到达这篇文章。在查看链接后,我了解到我必须在Azure AD中注册我的第三方应用程序作为Native Application
。因此,这两个应用程序都在Azure AD中注册。
我在Daemon or Server Application to Web API应用程序类型和场景下创建了我的应用程序。
问题在浏览了Bruce Chen提供的资源链接之后,我能够构建我的代码来接收令牌;事实上我得到了一个代币。但是,当我访问我想要显示的资源时,我看到了用户名和密码提示符被抛出。
我的解决方法
string aadInstance = "https://login.microsoftonline.com/{0}";
string tenant = "xxxx.onmicrosoft.com";
string clientId = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
string todoListResourceId = @"https://xxxxx.azurewebsites.net/Customer/CashSummary?term=673130569-VN/00";
string todoListBaseAddress = @"https://graph.windows.net";
AuthenticationContext authContext = null;
AuthenticationResult result = null;
authContext = new AuthenticationContext(authority, new FileCache());
UserCredential uc = new UserPasswordCredential("[email protected]", "xxxxxxxx");
try
{
// I am getting the Token here.
result = authContext.AcquireTokenAsync(todoListBaseAddress, clientId, uc).Result;
#region Call Web APP
//Here with the Token I am calling the MVC Web Resource View
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpResponseMessage response = httpClient.GetAsync(todoListResourceId).Result;
if (response.IsSuccessStatusCode)
{
// I am getting the response content as the Microsoft Identity Provider's User name and password Prompt instead of my MVC-View's HTML Content
string rezstring = response.Content.ReadAsStringAsync().Result;
var todoArray = JArray.Parse(rezstring);
Console.ForegroundColor = ConsoleColor.Green;
foreach (var todo in todoArray)
{
Console.WriteLine(todo["Title"]);
}
}
#endregion
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
return;
}
我的担忧
在Resource
的AcquireTokenAsync
;这里的Resource
是什么,是Graph API的URI还是APP ID的URI还是其他什么?
我使用过Graph API的URI,并且能够成功获取令牌。
感谢@Bruce-Chen的指导性解释。
资源应该是API的允许受众。
这应该是API的Application Id URI,或API的客户端ID。
如果您将https://graph.windows.net
指定为资源URI,则会获得一个访问令牌,该令牌可以调用Azure AD Graph API,但不能调用您的API。