Guest User

Untitled

a guest
Jan 28th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. result = authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword)).Result;
  2.  
  3. string hardcodedUsername = "username";
  4. string hardcodedPassword = "password";
  5.  
  6. string tenant = "tenantId@onmicrosoft.com";
  7. string clientId = "clientId";
  8. string resourceHostUri = "https://management.azure.com/";
  9. string aadInstance = "https://login.microsoftonline.com/{0}";
  10.  
  11. string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
  12.  
  13.  
  14. authContext = new AuthenticationContext(authority);
  15.  
  16. AuthenticationResult result = null;
  17. try
  18. {
  19.  
  20. result = authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword)).Result;
  21. }
  22. catch (Exception ex)
  23. {
  24. System.Diagnostics.Debug.WriteLine(ex.Message);
  25. }
  26.  
  27. return result;
  28.  
  29. // Get OAuth token using client credentials
  30. string tenantName = "GraphDir1.OnMicrosoft.com";
  31. string authString = "https://login.microsoftonline.com/" + tenantName;
  32.  
  33. AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
  34.  
  35. // Config for OAuth client credentials
  36. string clientId = "118473c2-7619-46e3-a8e4-6da8d5f56e12";
  37. string key = "hOrJ0r0TZ4GQ3obp+vk3FZ7JBVP+TX353kNo6QwNq7Q=";
  38. ClientCredential clientCred = new ClientCredential(clientId, key);
  39. string resource = "https://graph.windows.net";
  40. string token;
  41. try
  42. {
  43. AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, clientCred);
  44. token = authenticationResult.AccessToken;
  45. }
  46. catch (AuthenticationException ex)
  47. {
  48. Console.ForegroundColor = ConsoleColor.Red;
  49. Console.WriteLine("Acquiring a token failed with the following error: {0}", ex.Message);
  50. if (ex.InnerException != null)
  51. {
  52. // You should implement retry and back-off logic according to
  53. // http://msdn.microsoft.com/en-us/library/dn168916.aspx . This topic also
  54. // explains the HTTP error status code in the InnerException message.
  55. Console.WriteLine("Error detail: {0}", ex.InnerException.Message);
  56. }
  57. }
  58.  
  59. static void Main(string[] args)
  60. {
  61. Task<AuthenticationResult> t = getAccessToken();
  62. t.Wait();
  63. var result = t.Result;
  64. Console.WriteLine(result.AccessToken);
  65. Console.WriteLine("Please any key to terminate the program");
  66. Console.ReadKey();
  67. }
  68.  
  69. public static async Task<AuthenticationResult> getAccessToken()
  70. {
  71. string hardcodedUsername = "username";
  72. string hardcodedPassword = "password";
  73.  
  74. string tenant = "tenant.onmicrosoft.com";
  75. string clientId = "clientId";
  76. string resourceHostUri = "https://management.azure.com/";
  77. string aadInstance = "https://login.microsoftonline.com/{0}";
  78.  
  79. string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
  80.  
  81.  
  82. var authContext = new AuthenticationContext(authority);
  83.  
  84. AuthenticationResult result = null;
  85. try
  86. {
  87. result = await authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword));
  88. }
  89. catch (Exception ex)
  90. {
  91. Console.WriteLine(ex.StackTrace);
  92. System.Diagnostics.Debug.WriteLine(ex.Message);
  93. }
  94.  
  95. return result;
  96. }
Add Comment
Please, Sign In to add comment