所以我相信我的 APIservice 应该没问题,因为我可以通过 Swagger 返回结果?我从 WPF 项目中调用。我启动该程序并要求我登录,然后它会继续并告诉我我没有权限。
我对 WebAPI2 非常熟悉,我认为我可能只是错误地构建了我的调用。看来我确实从我的站点正确地获得了一个令牌,唯一的问题是当我尝试实际调用 API 以获取数据时。
这是我的代码:
public static string clientId = "{#Calling App Id}";
public static string commonAuthority = "https://login.windows.net/{#my Azure AD tenant}";
public static Uri returnUri = new Uri("http://MyDirectorySearcherApp");
const string ResourceUri = "https://{#Api App Service}.azurewebsites.net";
public static async Task<List<User>> LoadBands(IPlatformParameters parent)
{
AuthenticationResult authResult = null;
List<User> results = new List<User>();
try {
//get token or use refresh
AuthenticationContext authContext = new AuthenticationContext(commonAuthority);
if (authContext.TokenCache.ReadItems().Count() > 0)
authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
authResult = await authContext.AcquireTokenAsync(ResourceUri, clientId, returnUri, parent);
} catch (Exception ee) {
throw ex;
}
using (var httpClient = new HttpClient()) {
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, $"{ResourceUri}/api/Band/")) {
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
using (var response = await httpClient.SendAsync(request)) {
string responseData = await response.Content.ReadAsStringAsync();
//responseData always equals "You do not have permission to view this directory or page"
return results;
}
}
}
编辑:可能有助于注意我正在使用由 Rest API 调用的 DataAPI,其余 API 由 Azure AD 保护。
编辑:我从便携式类库中调用。
编辑:好吧,我正在通过身份验证,但似乎没有任何区别。如果我完全删除 Auth 标头,我会得到相同的结果