2016-10-06 59 views
0

通过code provided by Microsoft(我假设),我无法查询我的Azure Active Directory。每次我打电话下面,我得到的{Authorization Required.}响应:访问来自Azure Active Directory的响应

ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient(); 
IPagedCollection<IUser> pagedCollection = await client.Users.ExecuteAsync(); 

我是新Azure的Active Directory和我是新来的图形,并认为所提供的样本将发挥作用。他们不这样做,我希望这里有人能告诉我代码有什么问题,或者我如何授权我自己的目录?我认为AccessKey是认证方法,但显然这是没用的,因为它没有用在他们的例子中。

回答

2

基本上,要调用受Azure AD保护的支持OAuth2.0授权第三方应用程序的REST,我们需要传递一个承载令牌。

为了查看代码示例,请确保您按照README.md的步骤列表进行操作。

注意:README.md中有关于配置权限的内容尚不清楚。代码示例使用的是代替Microsoft Graph的Azure AD图表,我们需要选择Windows Azure Active Directory而不是Microsoft Graph。我已经报告了这个问题here

你可以看到有一个静态的申请命名,其中将设置当用户登录使用像下面Startup.Auth.cs代码值类AuthenticationHelper 令牌 :(不使用证书)

// Create a Client Credential Using an Application Key 
ClientCredential credential = new ClientCredential(clientId, appKey); 
string userObjectID = context.AuthenticationTicket.Identity.FindFirst(
    "http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 

AuthenticationContext authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectID)); 
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
            code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId); 

AuthenticationHelper.token = result.AccessToken; 

这里是通过OAuth 2.0用户码流批获得令牌的详细进展情况: enter image description here

更多细节这个流程可以参考here