Azure通过api认证过程401清除cdn

时间:2018-01-31 17:30:15

标签: azure azure-active-directory

我试图通过azure api清除缓存。

  1. 我创建了一个名为"清除CDN APP"的Azure Active Directory / App Registration。
  2. 权限:

    Windows Azure Active Directory /委托权限/登录并阅读用户个人资料

    Windows Azure服务管理API /委派权限/作为组织用户访问Azure服务管理(预览)

    键: 我创造了一把钥匙。我进入guid并返回一个密钥。我用这个作为我的客户秘密。

    1. 我有一个终点" CDN端点"。我添加了一个撰稿人"清除CDN APP"
    2. 我能够生成令牌。但是,我收到401错误。这里有很多步骤。你能指出我哪里出错了吗?我使用的客户ID来自应用注册应用ID。

         private static string GetAccessTokenAndMakePurgeCall()
         {
                  var subscriptionId = "xxxxx-xxxx-4dd7-8fec-ad3cd882b066";
                  var resourceGroup = "xxxHosting";
                  var profileName = "nameofcdn";
                  var endpointName = "nameofendpoint"; 
                  var apiVersion = "2016-10-02";
                  var url = string.Format(
                      "https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Cdn/profiles/{2}/endpoints/{3}/purge?api-version={4}", 
                      subscriptionId,
                      resourceGroup,
                      profileName,
                      endpointName,
                      apiVersion);
      
      
                  string clientId = ConfigurationManager.AppSettings["clientId"];
                  string clientSecret = ConfigurationManager.AppSettings["clientSecret"];
                  var authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/microsoft.onmicrosoft.com");
                  ClientCredential clientCredential = new ClientCredential(clientId, clientSecret);
                  Task<AuthenticationResult> resultstr = authenticationContext.AcquireTokenAsync("https://management.core.windows.net/", clientCredential);
      
                  WebClient client = new WebClient();
                  var token = resultstr.Result.AccessToken;
                  client.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);
                  client.Headers.Add(string.Format("api-version:{0}", apiVersion));
                  client.Headers.Add("Content-Type", "application/json");
      
                  var bodyText = "{\"ContentPaths\":[\"/*\"]}";
      
                  try
                  {
                      var result = client.UploadString(url, bodyText);
                      return result;
                  }
                  catch (Exception ew)
                  {
                      return ew.Message;
                  }
              }
      

0 个答案:

没有答案