首页 > 代码库 > Windows Azure服务管理请求验证

Windows Azure服务管理请求验证

    安全性考虑与设计,Windows Azure的服务请求必须通过安全认证,验证的方式有两种如下:

    1.Authenticate using Azure Active Directory(活动目录验证)

    Secure requests to the management service can be authenticated by creating an Azure AD application and using the Active Directory Authentication Library to obtain an access token from the application.

    

    a.Sign in to the Azure Management Portal.        

    b.Towards the bottom of the left menu, click Active Directory, and then click Default Directory.

    c.On the Default Directory page, under Integrate applications, click Add an application you’re developing.

wKiom1RheqmBbrpIAACY8ZbG20I520.jpg

    d.Enter the name of the application, select NATIVE CLIENT APPLICATION, and then click the right arrow. 

wKiom1Rhes_BDBOsAACqa6h5loI638.jpg

    e.Enter a URI for the application and then click the checkmark. The URI includes the name of the application that is preceded by http://localhost/.   

wKioL1Rhe2eiCq9ZAACEb7g_Ir4083.jpg

    f.After the application is created, you must add permission for the application to access the Service Management APIs. Go to the page for your application, and then click Configure.

wKiom1RheyCQ5edPAAC9it2Esns530.jpg  

    g.In the Permissions to other applications section at the bottom of the configuration page, click Select application, and then select Windows Azure Service Management API.

wKiom1RheyCQ5edPAAC9it2Esns530.jpg

    h.Click Delegated Permissions: 0, and then select Access Azure Service Management.

 wKioL1Rhe6jinpxsAADGZc5ZaCo407.jpg

    i.Click Save on the bottom menu.


    You can easily install the Active Directory Authentication Library into your Visual Studio project by using the NuGet package. To install the package, do the following:

    

    1.Right-click the project name in the Solution Explorer, and then click Manage NuGet Packages.

    wKioL1RhfULQenAIAAH5RkiqLzM285.jpg

    2.Type Active Directory in the search box, click Install for the Active Directory Authentication Library package, and then follow the instructions to install the package.

wKioL1Rhfa-DirYzAAMs9N_jxTg121.jpg

下面代码范例演示如何进行访问控制:

private static string GetAuthorizationHeader()
{
  AuthenticationResult result = null;
  
  var context = new AuthenticationContext("https://login.windows.net/{tenantId}");

  var thread = new Thread(() =>
  {
    result = context.AcquireToken(
      "https://management.core.windows.net/",
      "{clientId}",
      new Uri("{redirectUri}"));
  });

  thread.SetApartmentState(ApartmentState.STA);
  thread.Name = "AquireTokenThread";
  thread.Start();
  thread.Join();
            
  if (result == null)
  {
    throw new InvalidOperationException("Failed to obtain the JWT token");
  }

  string token = result.AccessToken;
  return token;
}

  3.

{tenantId} with the GUID of the application. To find the GUID, go to the Default Directory page in the Active Directory section of the Management Portal, select the application that you previously created, and then click View Endpoints.

    wKioL1Rh0srxeOgbAAAzT7T3sS4953.jpg

    4.Copy the GUID of the application and replace the placeholder with it.

    wKiom1Rh0qeRhrKMAAHBCeB3p2E314.jpg

    {clientId} with the client identifier. To find the client identifier, go to the Configuration page of the application in the Management Portal.

    wKiom1Rh0uvBhLJ_AADC6H6ZO-0285.jpg

    Use the following line of code to assign the token that is returned from the GetAuthorizationHeader method shown above to a variable that can be used by the request:

    

string token = GetAuthorizationHeader();

    Use the following lines of code to add the token to a request: 

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);


   2.Authenticate using a management certificate(管理证书验证)


   在说管理证书验证之前要先说一下以下两点:

    a.The Service Management API does not verify that a certificate is still valid. Authentication will succeed against an expired certificate.

    b.All management certificates carry the same set of privileges. There is no notion of “role-based” authentication where one management certificate can be configured in one role and another on the same subscription is configured in a different role. 


wKioL1Rh1NfjXSASAALwU7YYCW0264.jpg

如上面所展示:同一个订阅ID号有多个指纹,通过指纹就可以找到对应的管理证书进行验证;代码如下:

private static X509Certificate2 GetStoreCertificate(string thumbprint)
{
  List<StoreLocation> locations = new List<StoreLocation>
  { 
    StoreLocation.CurrentUser, 
    StoreLocation.LocalMachine
  };  
  foreach (var location in locations)
  {
    X509Store store = new X509Store("My", location);    try
    {
      store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
      X509Certificate2Collection certificates = store.Certificates.Find(
        X509FindType.FindByThumbprint, thumbprint, false);      if (certificates.Count == 1)
      {       
       return certificates[0];
      }
    }    finally
    {
      store.Close();
    }
  }  throw new ArgumentException(string.Format(    "A Certificate with Thumbprint ‘{0}‘ could not be located.",
    thumbprint));
}
//通过指纹获得存储的可用证书
X509Certificate2 certificate = GetStoreCertificate(Thumbprint);

把证书添加到请求头中:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.ClientCertificates.Add(certificate);



总结:

    版本控制提供了用户API版本的选择,管理认证让应用管理更加安全。

第一种方式:中国世纪互联代理没有提供这个服务。所以我也没法做个测试,烦人。

wKioL1Rh2DWh5CYTAADxd3TeG2w256.jpg

第二种方式是通过证书进行管理认证,接下来将单独写一个创建VM的Demo中会详细说明这个过程。另外上面的两种方式都是微软官方在API中可以找到详细说明的。(Windows Azure REST API)

本文出自 “爱咖啡” 博客,请务必保留此出处http://4837471.blog.51cto.com/4827471/1575445

Windows Azure服务管理请求验证