【发布时间】:2017-03-10 14:44:26
【问题描述】:
我正在尝试保护我的 Web API 应用程序,以便只有特定用户和应用程序才能使用这些服务。我遵循了许多不同的说明,这些说明建议我使用以下代码进行身份验证(我已将其简化为可以在控制台应用程序中轻松重现):
class Program
{
private const string ServicesClientId = "11111111-1111-1111-1111-111111111111";
private const string ClientId = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
private const string ClientKey = "abcdefghijklmnopqrstuvwxyz1234567890=";
private const string AadLoginUri = "https://login.windows.net/{0}";
private const string TenantId = "example.onmicrosoft.com";
private static readonly string Authority = string.Format(CultureInfo.InvariantCulture, AadLoginUri, TenantId);
static void Main(string[] args)
{
var clientCredential = new ClientCredential(ClientId, ClientKey);
var context = new AuthenticationContext(Authority, false);
// This line fails!
var appAuthResult = context.AcquireToken(ServicesClientId, clientCredential);
// AADSTS50105: Application 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' is not
// assigned to a role for the application '11111111-1111-1111-1111-111111111111'.
var appAuthTokenProvider = new ApplicationTokenProvider(context, "https://example.azurewebsites.net", clientCredential, appAuthResult);
var tokenCreds = new TokenCredentials(appAuthTokenProvider);
Console.WriteLine(tokenCreds.ToString());
Console.ReadLine();
}
}
因此,只要禁用用户分配但启用用户分配的那一刻,此代码就可以很好地工作(您等待一分钟,因为即使它说已成功启用,它似乎也不会立即生效),它失败。
我收到以下错误:
AADSTS50105:应用程序 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' 不是 分配给应用程序的角色 '11111111-1111-1111-1111-111111111111'。
我必须怎么做才能让它工作?
我已经尝试并尝试了各种方法来让这种情况在没有运气的情况下消失。我尝试过的事情:
- 将我注册客户端的默认“访问 MyWebAPI”权限添加到注册的 Web API 项目中
- 更改 Web API 应用程序的清单以添加应用程序角色:
{ "allowedMemberTypes": [ "Application" ], "displayName": "Universal App Client", "id": "c27e3fa1-e96a-445c-aaf7-8cbb60cca980", "isEnabled": true, "description": "Application Consuming all Services.", "value": "AppClient" },然后设置应用程序权限(在注册的消费应用程序上)拥有这个新的“通用应用程序客户端”权限。 - 更改 Web API 应用程序的清单以在
knownClientApplications数组下添加消费应用程序的客户端 ID。 - 我已经多次重新配置我的 Web API 应用程序,以防我在故障排除过程中搞砸了它,但我总是以失败告终
- 一边抚摸我的肚子一边拍拍我的头。
我不确定下一步该尝试什么。
为了参考,这里是我的 packages.config 文件:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.1" targetFramework="net46" />
<package id="Microsoft.Rest.ClientRuntime" version="1.8.2" targetFramework="net46" />
<package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="0.11.3" targetFramework="net46" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net46" />
</packages>
这个问题类似于this other question,但如上所述,该问题的答案(重新配置和重新部署)对我不起作用。
【问题讨论】:
标签: azure authentication azure-web-app-service azure-active-directory