【问题标题】:Recreate Az.Connect-AzAccount in c#在 c# 中重新创建 Az.Connect-AzAccount
【发布时间】:2019-12-22 00:28:06
【问题描述】:

PowerShell 在 Az.Accounts 库中具有出色的功能 - “Connect-AzAccount” - 它允许我针对我的 Azure 实例进行身份验证并轻松发布 APIM。我需要将代码移植到 c# 并且找不到简单的等价物。

有没有人找到一个简单的方法来做到这一点? Azure 世界/库似乎经常更新,以及许多安全方面。很难找到最近的相关示例。

我的最终目标是针对 Azure 进行身份验证,然后将 API 发布到 API 网关。

PowerShell 代码:

$user = "account@domain.com"
$password = ConvertTo-SecureString -String "password goes here" -AsPlainText -Force

$azAccount = @{
    subscription = "subscription-guid-goes-here"
    credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $user,$password
}

Connect-AzAccount @azAccount

【问题讨论】:

  • Azure REST API 有一个 .NET Library,但我从未使用过它,所以在这里不能说它是否是一个可行的选择。您可以将代码保存到脚本中,然后通过将其传递给PowerShell.exe 从 C# 间接执行它。或者,您可以使用 PowerShell Class 更直接地调用 PowerShell。

标签: c# azure powershell azure-api-management azure-authentication


【解决方案1】:

目前,Azure Management Library for .NET 只能用于管理部分 Azure 资源。但是,不包括 Azure API 管理。

所以,我建议您使用Azure REST API 来管理您的资源。

由于 Azure REST APi 受 Azure AD 保护。因此,第一步是获取访问令牌以进行身份​​验证。

这是使用 Microsoft.IdentityModel.Clients.ActiveDirectory 包的示例

static string GetToken()
{
    string tenantId = "your tenant id or name, for example: hanxia.onmicrosoft.com";
    string clientId = "1950a258-227b-4e31-a9cf-717495945fc2"; // it is a public client for every tenant. 
    string resource = "https://management.core.windows.net/";
    string username = "user name, jack@hanxia.onmicrosoft.com";
    string password = "password, D******";

    var upc = new UserPasswordCredential(username, password);
    var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId);
    AuthenticationResult result = context.AcquireTokenAsync(resource, clientId, upc).Result;
    return result.AccessToken;
}

之后,您可以调用 Azure REST API 并添加授权标头。这是一个 POST 请求示例:

public static string postRequest(string url, string access_token, string data)
{
    byte[] buffer = null;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "post";
    request.ContentType = "application/json";
    request.Headers.Add("Authorization", "Bearer " + access_token);
    //request.Headers.Add("other header", "it's value");
    if (data != null)
        buffer = Encoding.UTF8.GetBytes(data);
    else
        buffer = Encoding.UTF8.GetBytes("");
    request.ContentLength = buffer.Length;
    request.GetRequestStream().Write(buffer, 0, buffer.Length);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
    {
        return response.StatusCode + " " + reader.ReadToEnd();
    }
}

【讨论】:

  • 你可以建议他使用预发布包。但我没有。
  • 对,这就是不提的原因。说得通。最好重新创建 SDK。大声笑
【解决方案2】:

现有的答案有点奇怪,它链接了一些旧的折旧库。 Official library 支持 "managing" Api Management。这将是使用 C# 管理 Azure 资源的首选方式,您无需重新创建任何内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 2010-10-09
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多