Cloud Backup for IaaS + PaaS パブリック API で認証するアクセス トークンを取得します。アクセス トークンを取得するには、以下の属性を指定してください。
要素 |
説明 |
identityServiceUrl |
•https://identity.avepointonlineservices.com |
clientId |
AvePoint Online Services > システム管理 > アプリの登録 で登録したアプリのアプリケーション (クライアント) ID を指定します。 |
scope |
アプリに付与する権限を指定します。Cloud Backup for IaaS + PaaS の場合、値が platformbackup.readwrite.all になります。 |
certificateThumbprint |
アプリの登録時に使用した .cer 証明書の対応する .pfx 証明書ファイルの拇印です。 |
TokenLifetimeInMinutes |
取得したトークンの有効期限を指定します。時間の単位は分間です。 |
Var identityServiceUrl = “{https://identity.avepointonlineservices.com}”;
var client = new HttpClient();
var disco = await client.GetDiscoveryDocumentAsync(identityServiceUrl);
if (disco.IsError)
{
return;
}
var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientAssertion = new ClientAssertion()
{
Type = OidcConstants.ClientAssertionTypes.JwtBearer,
Value = CreateClientAuthJwt(disco)
},
Scope = “platformbackup.readwrite.all”,
}
if (tokenResponse.IsError)
{
return;
}
return tokenResponse.Json
private static string CreateClientAuthJwt(DiscoveryDocumentResponse response)
{
var clientId = “{Client ID}”;
var certificateThumbprint = “{Certificate Thumbprint}”;
// Sets the token to expire in 5 minutes.
var tokenHandler = new JwtSecurityTokenHandler { TokenLifetimeInMinutes = 5 };
var securityToken = tokenHandler.CreateJwtSecurityToken(
issuer: clientId,
audience: response.TokenEndpoint,
subject: new ClaimsIdentity(
new List<Claim> { new Claim(“sub”, clientId),
new Claim(“jti”, Guid.NewGuid().ToString())}),
signingCredentials: new SigningCredentials(
new X509SecurityKey(new X509Certificate2(LoadCertificate(certificateThumbprint))), “RS256”)
);
return tokenHandler.WriteToken(securityToken);
}
private static X509Certificate2 LoadCertificate(string certificateThumbprint)
{
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var vCloudCertificate = store.Certificates.Find(
X509FindType.FindByThumbprint,
certificateThumbprint,
false)[0];
return vCloudCertificate;
}