c# - How to create serviceclientcredential to be used with Microsoft.azure.Management.compute -
i trying programmatically retrieve hostedservices microsoft.azure.management.compute using c#. requires serviceclientcredential , not know how it. can please me? able them using microsoft.windowsazure.management.compute. here returns instances under resourcemanager not classic instances.
thanks in advance.
jayashri
1st need create active directory application. use link create ad app https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-service-principal-portal/
sample code use microsoft.azure.management.compute 13.0.1-prerelease sdk url: https://www.nuget.org/packages/microsoft.azure.management.compute/13.0.1-prerelease
public class customlogincredentials : serviceclientcredentials { private string authenticationtoken { get; set; } public override void initializeserviceclient<t>(serviceclient<t> client) { var authenticationcontext = new authenticationcontext("https://login.windows.net/{tenantid}"); var credential = new clientcredential(clientid: "xxxxx-xxxx-xx-xxxx-xxx", clientsecret: "{clientsecret}"); var result = authenticationcontext.acquiretoken(resource: "https://management.core.windows.net/", clientcredential: credential); if (result == null) { throw new invalidoperationexception("failed obtain jwt token"); } authenticationtoken = result.accesstoken; } public override async task processhttprequestasync(httprequestmessage request, cancellationtoken cancellationtoken) { if (request == null) { throw new argumentnullexception("request"); } if (authenticationtoken == null) { throw new invalidoperationexception("token provider cannot null"); } request.headers.authorization = new authenticationheadervalue("bearer", authenticationtoken); request.headers.accept.add(new mediatypewithqualityheadervalue("application/json")); //request.version = new version(apiversion); await base.processhttprequestasync(request, cancellationtoken); } }
and here how initialize client
netclient = new microsoft.azure.management.compute.computemanagementclient(new customlogincredentials()); netclient.subscriptionid = _subscriptionid;
Comments
Post a Comment