Simplest way to access azure programmatically

2019-04-10 01:49发布

I have the following code:

var client = new WebSiteManagementClient(creds);
var data = client.WebSites.Get("eastuswebspace", "some-site", new WebSiteGetParameters());

I need to generate credentials, I am an admin with full access. Only one App accessing my data and need to generate a TokenCloudCredentials.

I tried registering an App but getting a "forbidden" error after successfully generating the token. Is there a simplest way of doing this (like Github, generate a token and it's done?).

Thanks.

2条回答
Luminary・发光体
2楼-- · 2019-04-10 01:59

I managed to make it work with the following snippet. It just needs the certificate. Check if that works for you. You will need the Azure publishing settings file (thumbprint from that).

public const string base64EncodedCertificate = "frompublishsettingsfile";
    public const string subscriptionId = "";

    static SubscriptionCloudCredentials getCredentials()
    {
        return new CertificateCloudCredentials(subscriptionId, new X509Certificate2(Convert.FromBase64String(base64EncodedCertificate)));
    }
    static void Main(string[] args)
    {
        WebSiteManagementClient client = new WebSiteManagementClient(getCredentials());
查看更多
对你真心纯属浪费
3楼-- · 2019-04-10 02:01

To generate a Token Credentials, you need to create a certificate and upload it on Azure.

First, you can create a certificate using this command line :

makecert -sky exchange -r -n "CN=<CertificateName>" -pe -a sha1 -len 2048 -ss My "<CertificateName>.cer"

Then, it's necessary to upload it on Azure by following this process. Once it's uploaded, you can get the publishsettings file to obtain the certificate management thumbprint with this power shell command line :

Get-AzurePusblishSettingsFile

The following screen shot shows where to find the certificate management thumbprint in the publishprofile :

enter image description here

Then you can use the code proposed by Alex Belotserkovskiy to get authenticated with the Azure Management SDK.

Here is a complete blog post which shows all steps.

查看更多
登录 后发表回答