fcs-azure/README.md

51 lines
1.7 KiB
Markdown
Raw Normal View History

2022-12-14 11:57:22 +01:00
# fcs-azure
Library to aquire Azure access token
2022-12-14 12:01:50 +01:00
Sample call
2022-12-14 12:05:10 +01:00
```csharp
2022-12-14 12:04:16 +01:00
using System;
using FCS.Lib.Azure;
using FCS.Lib.Utility;
namespace TestAzure
2022-12-14 12:01:50 +01:00
{
2022-12-14 12:04:16 +01:00
public class Program
2022-12-14 12:01:50 +01:00
{
2022-12-14 12:04:16 +01:00
static void Main(string[] args)
{
var settings = new MySettings();
2022-12-14 12:01:50 +01:00
2022-12-14 12:04:16 +01:00
var authStore = new AzureAuthStore(settings.LoginUrl, settings.OAuthEndpoint, settings.TenantId,
settings.ClientId, settings.GrantType, settings.ClientSecret, settings.LoginScope);
2022-12-14 12:01:50 +01:00
2022-12-14 12:04:16 +01:00
var tokenFetcher = new AzureTokenFetcher(authStore);
2022-12-14 12:01:50 +01:00
2022-12-14 12:04:16 +01:00
// normally called async - but this is only a test
var token = tokenFetcher.FetchAzureToken().Result;
2022-12-14 12:01:50 +01:00
2022-12-14 12:04:16 +01:00
var ts1 = Mogrify.CurrentDateTimeToTimeStamp();
var ts2 = Mogrify.DateTimeToTimeStamp(DateTime.Now.AddHours(+2));
2022-12-14 12:01:50 +01:00
2022-12-14 12:04:16 +01:00
Console.WriteLine($"AccessToken: {token.AccessToken}");
Console.WriteLine($"Expires : {token.Expires}");
Console.WriteLine($"TokenType : {token.TokenType}");
Console.WriteLine($"HasExpired : {DateTime.Now} {ts1} {token.HasExpired(ts1)}");
Console.WriteLine($"HasExpired : {DateTime.Now.AddHours(+2)} {ts2} {token.HasExpired(ts2)}");
Console.ReadKey();
}
2022-12-14 12:01:50 +01:00
}
2022-12-14 12:04:16 +01:00
internal sealed class MySettings
{
public string LoginUrl => "https://login.microsoftonline.com";
public string OAuthEndpoint => "oauth2/v2.0/token";
public string GrantType => "client_credentials";
public string LoginScope => "insert-your-login-scope";
public string TenantId => "insert-your-tenant-id";
public string ClientId => "insert-your-client-id";
public string ClientSecret => "insert-your-client-secret";
}
2022-12-14 12:01:50 +01:00
}
```