-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAuthUtil.cs
40 lines (35 loc) · 1.21 KB
/
AuthUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace azure_usage_report
{
using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
public static class AuthUtil
{
private const string Resource = "https://management.azure.com/";
public static AuthenticationResult GetToken(string tenantId, string clientId, string clientSecret)
{
LoggerCallbackHandler.UseDefaultLogging = false;
var ctx = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId);
var credential = new ClientCredential(clientId, clientSecret);
AuthenticationResult result = null;
try
{
result = ctx.AcquireTokenAsync(AuthUtil.Resource, credential).Result;
}
catch (Exception ex)
{
var adalEx = ex.InnerException as AdalException;
if (adalEx != null)
{
Trace.TraceInformation($"Error code: {adalEx.ErrorCode}. Messsage: {adalEx.Message}.");
}
else
{
throw;
}
}
return result;
}
}
}