Skip to content

Instantly share code, notes, and snippets.

View cjvandyk's full-sized avatar

Cornelius J. van Dyk cjvandyk

View GitHub Profile
@cjvandyk
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)
.WithAuthority($"https://login.microsoftonline.com/{tenantId}")
.Build();
@cjvandyk
cjvandyk / MSAL-in-JavaScript
Created April 19, 2023 12:29
MSAL in JavaScript
const msalConfig = {
auth: {
clientId: 'your-client-id',
authority: 'https://login.microsoftonline.com/common',
redirectUri: 'http://localhost:3000',
},
cache: {
cacheLocation: 'localStorage',
storeAuthStateInCookie: true,
}
@cjvandyk
cjvandyk / MSAL-in-CSharp
Created April 19, 2023 12:22
MSAL in C#
string[] scopes = { "user.read" };
string clientId = "your-client-id";
string redirectUri = "http://localhost/myapp";
IPublicClientApplication app = PublicClientApplicationBuilder
.Create(clientId)
.WithRedirectUri(redirectUri)
.Build();
AuthenticationResult result = await app.AcquireTokenInteractive(scopes)
.ExecuteAsync();
@cjvandyk
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.
Console.WriteLine(diff.TotalSeconds);
@cjvandyk
cjvandyk / AddNetStandardReference.xml
Created April 9, 2020 18:53
NetStandard reference
<configuration>
<system.web>
<compilation>
<assemblies>
<add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"/>
</assemblies>
</compilation>
</system.web>
</configuration>
@cjvandyk
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
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", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2014-07-12T15:29:09");
@cjvandyk
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
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
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.