【问题标题】:Azure Pack REST API AuthenticationAzure 包 REST API 身份验证
【发布时间】:2016-08-28 00:48:28
【问题描述】:

在 Microsoft 中搜索数小时后弄乱了其产品的 API 文档,我仍然不知道如何在 Windows azure pack 分发中验证 rest API 请求。 主要是我想创建一个 API 来自动化部署虚拟机的过程,但我找不到任何关于如何获取身份验证令牌来访问资源的文档。

一些文档说明了 ADFS 的使用,但没有提供任何关于 ADFS REST API 进行身份验证的参考。

而且我一开始不想使用 ADFS。我想使用 AZURE 租户和管理界面进行身份验证。

总之,如果有人可以提供有关 REST API 身份验证的任何帮助,那将是我的一天。 提前致谢。

【问题讨论】:

    标签: api rest azure authentication azure-pack


    【解决方案1】:

    您可以使用以下 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
    

    【讨论】:

    • 嘿杰克...是用于 Azure 包还是堆栈?我特别想找 azure pack...
    • 我相信除了端点和资源之外它们是相同的。您可以使用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”是您的租户身份验证站点 url。 默认端口为 30071。

    你可以在这里找到一些资源: https://msdn.microsoft.com/en-us/library/dn479258.aspx

    示例程序“SampleAuthApplication”可以解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      相关资源
      最近更新 更多