设为首页 收藏本站
查看: 1671|回复: 0

[经验分享] Windows Azure服务管理请求验证

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-11-12 08:42:45 | 显示全部楼层 |阅读模式
  安全性考虑与设计,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.


    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
下面代码范例演示如何进行访问控制:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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:

1
string token = GetAuthorizationHeader();



    Use the following lines of code to add the token to a request:
1
2
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号有多个指纹,通过指纹就可以找到对应的管理证书进行验证;代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private static X509Certificate2 GetStoreCertificate(string thumbprint)
{
  List locations = new List
  {
    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));
}



1
2
//通过指纹获得存储的可用证书
X509Certificate2 certificate = GetStoreCertificate(Thumbprint);



把证书添加到请求头中:
1
2
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.ClientCertificates.Add(certificate);





总结:
    版本控制提供了用户API版本的选择,管理认证让应用管理更加安全。
第一种方式:中国世纪互联代理没有提供这个服务。所以我也没法做个测试,烦人。

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


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-28191-1-1.html 上篇帖子: Windows Azure服务管理版本控制 下篇帖子: 通过SSMS工具迁移本地的SQL Server Database到Windows Azure SQL Database Windows
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表