Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- result = authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword)).Result;
- string hardcodedUsername = "username";
- string hardcodedPassword = "password";
- string tenant = "tenantId@onmicrosoft.com";
- string clientId = "clientId";
- string resourceHostUri = "https://management.azure.com/";
- string aadInstance = "https://login.microsoftonline.com/{0}";
- string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
- authContext = new AuthenticationContext(authority);
- AuthenticationResult result = null;
- try
- {
- result = authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword)).Result;
- }
- catch (Exception ex)
- {
- System.Diagnostics.Debug.WriteLine(ex.Message);
- }
- return result;
- // Get OAuth token using client credentials
- string tenantName = "GraphDir1.OnMicrosoft.com";
- string authString = "https://login.microsoftonline.com/" + tenantName;
- AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
- // Config for OAuth client credentials
- string clientId = "118473c2-7619-46e3-a8e4-6da8d5f56e12";
- string key = "hOrJ0r0TZ4GQ3obp+vk3FZ7JBVP+TX353kNo6QwNq7Q=";
- ClientCredential clientCred = new ClientCredential(clientId, key);
- string resource = "https://graph.windows.net";
- string token;
- try
- {
- AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, clientCred);
- token = authenticationResult.AccessToken;
- }
- catch (AuthenticationException ex)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Acquiring a token failed with the following error: {0}", ex.Message);
- if (ex.InnerException != null)
- {
- // You should implement retry and back-off logic according to
- // http://msdn.microsoft.com/en-us/library/dn168916.aspx . This topic also
- // explains the HTTP error status code in the InnerException message.
- Console.WriteLine("Error detail: {0}", ex.InnerException.Message);
- }
- }
- static void Main(string[] args)
- {
- Task<AuthenticationResult> t = getAccessToken();
- t.Wait();
- var result = t.Result;
- Console.WriteLine(result.AccessToken);
- Console.WriteLine("Please any key to terminate the program");
- Console.ReadKey();
- }
- public static async Task<AuthenticationResult> getAccessToken()
- {
- string hardcodedUsername = "username";
- string hardcodedPassword = "password";
- string tenant = "tenant.onmicrosoft.com";
- string clientId = "clientId";
- string resourceHostUri = "https://management.azure.com/";
- string aadInstance = "https://login.microsoftonline.com/{0}";
- string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
- var authContext = new AuthenticationContext(authority);
- AuthenticationResult result = null;
- try
- {
- result = await authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword));
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.StackTrace);
- System.Diagnostics.Debug.WriteLine(ex.Message);
- }
- return result;
- }
Add Comment
Please, Sign In to add comment