2016-05-03 202 views
2

經過幾個小時的微軟搜索其產品的API文檔,我仍然沒有在哪裏如何驗證Windows Azure Pack發佈中的其他API請求。 主要我想創建一個API來自動執行部署虛擬機的過程,但是我找不到任何有關如何獲取身份驗證令牌來訪問資源的文檔。Azure Pack REST API身份驗證

某些文檔聲明使用ADFS,但不提供有關用於身份驗證的ADFS REST API的任何參考。

而我並不想首先使用ADFS。我想使用AZURE租戶和管理界面進行身份驗證。

總之,如果任何人都可以提供任何關於REST API認證的幫助,它將會使我的一天。 在此先感謝。

回答

0

您可以使用以下PowerShell來獲取訪問令牌。

Add-Type -Path 'C:\Program Files\Microsoft Azure Active Directory Connect\Microsoft.IdentityModel.Clients.ActiveDirectory.dll' 

$tenantID = "<the tenant id of you subscription>" 
$authString = "https://login.windows.net/$tenantID" 

# It must be an MFA-disabled admin. 
$username = "<the username>" 
$password = "<the password>" 

# The resource can be https://graph.windows.net/ if you are using graph api. 
# Or, https://management.azure.com/ if you are using ARM. 
$resource = "https://management.core.windows.net/" 

# This is the common client id. 
$client_id = "1950a258-227b-4e31-a9cf-717495945fc2" 

$creds = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" ` 
    -ArgumentList $username,$password 

$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" ` 
    -ArgumentList $authString 

$authenticationResult = $authContext.AcquireToken($resource,$client_id,$creds) 

# An Authorization header can be formed like this. 
$authHeader = $authenticationResult.AccessTokenType + " " + $authenticationResult.AccessToken 
+0

嘿傑克...是Azure包還是堆棧?我特別尋找天藍色包裝... –

+0

我相信它們除了端點和資源以外都是一樣的。你可以使用'Get-MgmtSvcToken'並添加'-Debug'參數來檢查。 –

-2

我正在做一些和你一樣的工作。

 static string GetAspAuthToken(string authSiteEndPoint, string userName, string password) 
    { 

     var identityProviderEndpoint = new EndpointAddress(new Uri(authSiteEndPoint + "/wstrust/issue/usernamemixed")); 

     var identityProviderBinding = new WS2007HttpBinding(SecurityMode.TransportWithMessageCredential); 
     identityProviderBinding.Security.Message.EstablishSecurityContext = false; 
     identityProviderBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; 
     identityProviderBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; 

     var trustChannelFactory = new WSTrustChannelFactory(identityProviderBinding, identityProviderEndpoint) 
     { 
      TrustVersion = TrustVersion.WSTrust13, 
     }; 
     //This line is only if we're using self-signed certs in the installation 
     trustChannelFactory.Credentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication() { CertificateValidationMode = X509CertificateValidationMode.None }; 

     trustChannelFactory.Credentials.SupportInteractive = false; 
     trustChannelFactory.Credentials.UserName.UserName = userName; 
     trustChannelFactory.Credentials.UserName.Password = password; 

     var channel = trustChannelFactory.CreateChannel(); 
     var rst = new RequestSecurityToken(RequestTypes.Issue) 
     { 
      AppliesTo = new EndpointReference("http://azureservices/TenantSite"), 
      TokenType = "urn:ietf:params:oauth:token-type:jwt", 
      KeyType = KeyTypes.Bearer, 
     }; 

     RequestSecurityTokenResponse rstr = null; 
     SecurityToken token = null; 


     token = channel.Issue(rst, out rstr); 
     var tokenString = (token as GenericXmlSecurityToken).TokenXml.InnerText; 
     var jwtString = Encoding.UTF8.GetString(Convert.FromBase64String(tokenString)); 

     return jwtString; 
    } 

參數「authSiteEndPoint」是您的租戶認證網站網址。 默認端口爲30071.

您可以在這裏找到一些資源: https://msdn.microsoft.com/en-us/library/dn479258.aspx

例子程序「SampleAuthApplication」可以解決你的問題。