Skip to content

Instantly share code, notes, and snippets.

View cjvandyk's full-sized avatar

Cornelius J. van Dyk cjvandyk

View GitHub Profile
cjvandyk / ModernAuth-with-MSAL-Device-Authentication-Flow
Created April 19, 2023 12:47
ModernAuth with MSAL Device Authentication Flow
using Microsoft.Identity.Client;
using System;
using System.Net.Http;
using System.Threading.Tasks;
public static async Task<string> GetAccessTokenAsync(string clientId, string tenantId)
var app = PublicClientApplicationBuilder.Create(clientId)
cjvandyk / MSAL-in-JavaScript
Created April 19, 2023 12:29
MSAL in JavaScript
const msalConfig = {
auth: {
clientId: 'your-client-id',
authority: '',
redirectUri: 'http://localhost:3000',
cache: {
cacheLocation: 'localStorage',
storeAuthStateInCookie: true,
cjvandyk / MSAL-in-CSharp
Created April 19, 2023 12:22
MSAL in C#
string[] scopes = { "" };
string clientId = "your-client-id";
string redirectUri = "http://localhost/myapp";
IPublicClientApplication app = PublicClientApplicationBuilder
AuthenticationResult result = await app.AcquireTokenInteractive(scopes)
cjvandyk / CalculateTimeSpan.cs
Created April 9, 2020 19:07
TimeSpan between two dates.
DateTime startTime = DateTime.Now;
//Code here
TimeSpan diff = DateTime.Now - startTime;
//Use methods in [diff] to get required data.
// e.g. the below is to get time in seconds.
cjvandyk / AddNetStandardReference.xml
Created April 9, 2020 18:53
NetStandard reference
<add assembly="netstandard, Version=, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"/>
cjvandyk / RetrieveLoaderExceptions.cs
Created April 9, 2020 15:12
Retrieve LoaderExceptions
catch (Exception ex)
if (ex is System.Reflection.ReflectionTypeLoadException)
var typeEx = ex as ReflectionTypeLoadException;
var loadEx = typeEx.LoaderExceptions;
//Do something with loadEx
cjvandyk / getInfoPathAttachments.cs
Created December 13, 2018 14:18
Consuming an InfoPath document and a specified node path, decode all file attachments from the document and return them in a list of memory streams.
public List<System.IO.MemoryStream> getInfoPathAttachments(System.Xml.XmlDocument InfoPathDocument, string nodePath)
/// <summary>
/// Grab multiple attachments from an InfoPath form and
/// return them in a list of memory streams.
/// </summary>
List<System.IO.MemoryStream> files = new List<MemoryStream>();
System.Xml.XmlNamespaceManager nm = new System.Xml.XmlNamespaceManager(InfoPathDocument.NameTable);
nm.AddNamespace("my", "");
cjvandyk / Save-Decoded-Attachment.cs
Created December 12, 2018 21:01
Save decoded attachment
//Create an XmlNamespaceManager
XmlNamespaceManager ns = this.NamespaceManager;
//Create an XPathNavigator object for the Main data source
XPathNavigator xnMain = this.MainDataSource.CreateNavigator();
//Create an XPathNavigator object for the attachment node
XPathNavigator xnAttNode = xnMain.SelectSingleNode("/my:myFields/my:theAttachmentField", ns);
//Obtain the text of the node.
cjvandyk / InfoPath-Attachment-Decoder.cs
Created December 12, 2018 20:50
InfoPath File Attachment Decoder
using System;
using System.IO;
using System.Text;
namespace InfoPathAttachmentEncoding
/// <summary>
/// Decodes a file attachment and saves it to a specified path.
/// </summary>
public class InfoPathAttachmentDecoder
cjvandyk / InfoPath-Attachment-Encoder.cs
Last active January 4, 2024 06:50
InfoPath Attachment Encoder Code
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using InfoPathAttachmentEncoding;
namespace InfoPathAttachmentEncoding
/// <summary>
/// InfoPathAttachment encodes file data into the format expected by InfoPath for use in file attachment nodes.