mirror of
https://github.com/Micke-K/IntuneManagement.git
synced 2026-09-28 10:55:38 +02:00
IntuneManagement 4.0.0-beta1
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using Microsoft.Identity.Client;
|
||||
|
||||
public class HttpFactoryWithProxy : IMsalHttpClientFactory
|
||||
{
|
||||
private static HttpClient _httpClient;
|
||||
|
||||
public HttpFactoryWithProxy(string proxyURI) : this(proxyURI, null, null)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public HttpFactoryWithProxy(string proxyURI, string proxyUserName = null, string proxyPassword = null)
|
||||
{
|
||||
if (_httpClient == null)
|
||||
{
|
||||
var proxy = new WebProxy
|
||||
{
|
||||
Address = new Uri(proxyURI),
|
||||
BypassProxyOnLocal = false,
|
||||
UseDefaultCredentials = false,
|
||||
Credentials = new NetworkCredential(
|
||||
userName: proxyUserName,
|
||||
password: proxyPassword)
|
||||
};
|
||||
|
||||
var httpClientHandler = new HttpClientHandler
|
||||
{
|
||||
Proxy = proxy,
|
||||
};
|
||||
|
||||
_httpClient = new HttpClient(handler: httpClientHandler);
|
||||
}
|
||||
}
|
||||
|
||||
public HttpClient GetHttpClient()
|
||||
{
|
||||
return _httpClient;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// Updated original code from
|
||||
// Added support for custom file location
|
||||
// https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-net-token-cache-serialization#simple-token-cache-serialization-msal-only
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using Microsoft.Identity.Client;
|
||||
|
||||
public static class TokenCacheHelperEx
|
||||
{
|
||||
public static void EnableSerialization(ITokenCache tokenCache, String fileName = @"%LOCALAPPDATA%\GraphPowerShellManager\MSALToken.bin")
|
||||
{
|
||||
tokenCache.SetBeforeAccess(BeforeAccessNotification);
|
||||
tokenCache.SetAfterAccess(AfterAccessNotification);
|
||||
|
||||
CacheFilePath = Environment.ExpandEnvironmentVariables(fileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Path to the token cache
|
||||
/// </summary>
|
||||
|
||||
public static string CacheFilePath { get; private set;}
|
||||
|
||||
private static readonly object FileLock = new object();
|
||||
|
||||
private static void BeforeAccessNotification(TokenCacheNotificationArgs args)
|
||||
{
|
||||
lock (FileLock)
|
||||
{
|
||||
args.TokenCache.DeserializeMsalV3(File.Exists(CacheFilePath)
|
||||
? ProtectedData.Unprotect(File.ReadAllBytes(CacheFilePath),
|
||||
null,
|
||||
DataProtectionScope.CurrentUser)
|
||||
: null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AfterAccessNotification(TokenCacheNotificationArgs args)
|
||||
{
|
||||
// if the access operation resulted in a cache update
|
||||
if (args.HasStateChanged)
|
||||
{
|
||||
lock (FileLock)
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(CacheFilePath));
|
||||
// reflect changes in the persistent store
|
||||
File.WriteAllBytes(CacheFilePath,
|
||||
ProtectedData.Protect(args.TokenCache.SerializeMsalV3(),
|
||||
null,
|
||||
DataProtectionScope.CurrentUser)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user