Rin*_*inu 11 c# azure office365 azure-active-directory
我必须针对Azure AD验证应用程序.我创建了Web API并将其添加到Azure AD应用程序部分.更改了清单文件,创建了Web API并使用Azure AD进行了身份验证,并创建了一个Windows窗体,其中包含以下代码:
private async void button1_Click(object sender, EventArgs e)
{
string authority = "https://login.windows.net/test113.onmicrosoft.com";
string resourceURI = "https://test113.onmicrosoft.com/ftp";
string clientID = "5177ef76-cbb4-43a8-a7d0-899d3e886b34";
Uri returnURI = new Uri("http://keoftp");
AuthenticationContext authContext =
new AuthenticationContext(authority);
AuthenticationResult authResult =
authContext.AcquireToken(resourceURI, clientID, returnURI);
string authHeader = authResult.CreateAuthorizationHeader();
// don't do this in prod
System.Net.ServicePointManager.ServerCertificateValidationCallback =
((s, c, c2, se) => true);
HttpClient client = new HttpClient();
HttpRequestMessage request =
new HttpRequestMessage(HttpMethod.Get, "https://localhost:44300/api/tasks");
request.Headers.TryAddWithoutValidation("Authorization", authHeader);
var response = await client.SendAsync(request);
string responseString = await response.Content.ReadAsStringAsync();
MessageBox.Show(responseString);
}
Run Code Online (Sandbox Code Playgroud)
我有一个例外:
Microsoft.IdentityModel.Clients.ActiveDirectory.dll中出现"Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException"类型的异常,但未在用户代码中处理
其他信息:AADSTS50001:在名为test113.onmicrosoft.com的租户中找不到名为https://test113.onmicrosoft.com/ftp的应用程序 .如果租户的管理员尚未安装应用程序或租户中的任何用户同意该应用程序,则会发生这种情况.您可能已将您的身份验证请求发送给错误的租户.
跟踪ID:e782d60e-b861-46a3-b32b-f3df78396bd0相关ID:b4809815-2755-4de1-bd1b-0221d74fd0f0时间戳:2016-03-17 11:20:08Z
Nan*_* Yu 11
请求中的资源表示您要在特定租户中访问的资源.当本机客户端需要从Azure Active Directory获取令牌时,它需要指定它需要令牌的资源.在此方案中,客户端应用程序需要访问Web API,因此Web API的APP ID URI将用作资源名称.在拥有令牌之后,它还需要知道可以访问资源的URL,在这种情况下是Web API的地址.例如:
// Resource settings this application wants to access
private string resource = "https://cloudalloc.com/CloudAlloc.WebAPI";
private Uri WebAPIUri = new Uri("https://localhost:44313");
Run Code Online (Sandbox Code Playgroud)
这两个设置都可以在Azure管理门户中的Web API应用程序的CONFIGURE页面的单点登录部分找到.
点击此处了解更多详情.
| 归档时间: |
|
| 查看次数: |
15171 次 |
| 最近记录: |