When I run this, I get an exception saying the request can't be authenticated. The exception also mentions a certificate, but not sure what certificate it's referring to Anyone experience this issue?
static void Main(string[] args) { const string subscriptionId = "XXXXX-XXXXXX-XXXXXX-XXXXX"; var token = GetAuthorizationHeader(); var credential = new TokenCloudCredentials(subscriptionId, token); // This will throw Microsoft.WindowsAzure.CloudException CreateStorageAccountAsync(credential).Wait(); } static string GetAuthorizationHeader() {
// These URLs have been stripped from the code sample because forum won't let me post links. const string apiEndPoint = @""; string azureAuthEndPoint = @""; string clientId = @"XXXXXXXXXX-XXXXXX-XXXXXXXXX"; string clientSecret = @"XXXXXXXXXXXXXXXXXXXXXXXXXX"; var context = new AuthenticationContext(azureAuthEndPoint); var clientCred = new ClientCredential(clientId, clientSecret); var result = context.AcquireToken(apiEndPoint, clientCred); if (result == null) { throw new InvalidOperationException("Failed to obtain the JWT token"); } string token = result.AccessToken; return token; } static async Task<string> CreateStorageAccountAsync(SubscriptionCloudCredentials credentials) { using (var storageClient = new StorageManagementClient(credentials)) { await storageClient.StorageAccounts.CreateAsync( new StorageAccountCreateParameters { Label = "Sample Storage Account", Location = LocationNames.WestUS, Name = "somerandomname" }); } return "Successfully created account."; }
System.AggregateException
was unhandled
HResult=-2146233088
Message=One or more errors occurred.
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task.Wait(Int32
millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at AzureCreateRelay.Program.Main(String[]
args) in d:\company\next03\shared\online\source\DevOps\Azure\AzureCreateRelay\Program.cs:line
23
at
System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at
System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
at
System.Threading.ExecutionContext.Run(ExecutionContext executionContext,
ContextCallback callback, Object state, Boolean preserveSyncCtx)
at
System.Threading.ExecutionContext.Run(ExecutionContext executionContext,
ContextCallback callback, Object state)
at
System.Threading.ThreadHelper.ThreadStart()
InnerException: Microsoft.WindowsAzure.CloudException
HResult=-2146233088
Message=ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.
Source=Microsoft.Threading.Tasks
ErrorCode=ForbiddenError
ErrorMessage=The server failed to
authenticate the request. Verify that the certificate is valid and is
associated with this subscription.
StackTrace:
at
Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task
task)
at
Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at
Microsoft.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at
Microsoft.WindowsAzure.Management.Storage.StorageAccountOperations.<CreateAsync>d__25.MoveNext()
--- End of stack trace from
previous location where exception was thrown ---
at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)
at
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at
AzureCreateRelay.Program.<CreateStorageAccountAsync>d__1.MoveNext() in
d:\company\next03\shared\online\source\DevOps\Azure\AzureCreateRelay\Program.cs:line
51
InnerException: <o:p></o:p>