Skip to content

Commit 628393e

Browse files
committed
Using the Microsoft Authentication Library (MSAL) with D365 Finance and Operations apps
1 parent 677ebbd commit 628393e

7 files changed

Lines changed: 183 additions & 60 deletions

File tree

CustomServiceTestUtil/Classes/AuthenticationHelper.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using Microsoft.IdentityModel.Clients.ActiveDirectory;
1+
using Microsoft.Identity.Client;
22
using System;
33
using System.Configuration;
44
using System.Net.Http.Headers;
5-
5+
using System.Threading.Tasks;
66
namespace CustomServiceTestUtil
77
{
88
/// <summary>
@@ -25,28 +25,26 @@ public static bool DropAuthheader
2525
}
2626

2727
/// <summary>
28-
/// Property that gets the AuthorizationHeader
28+
/// Method that gets the AuthorizationHeader
2929
/// </summary>
30-
public static string AuthorizationHeader
30+
public static async Task<string> GetAuthorizationHeader()
3131
{
32-
get
33-
{
34-
ServerSettings serverSettings = Settings.GetServerSettings();
35-
if (string.IsNullOrEmpty(authorizationHeader) || DateTime.UtcNow.AddSeconds(60) >= AuthenticationResult.ExpiresOn)
36-
{
37-
UriBuilder uri = new UriBuilder(serverSettings.AzureAuthEndpoint)
38-
{
39-
Path = serverSettings.AADTenant
40-
};
4132

42-
AuthenticationContext authenticationContext = new AuthenticationContext(uri.ToString());
43-
var credential = new ClientCredential(serverSettings.WebAppId, serverSettings.WebAADKey);
44-
AuthenticationResult = authenticationContext.AcquireTokenAsync(serverSettings.Ax7Endpoint, credential).Result;
45-
authorizationHeader = AuthenticationResult.CreateAuthorizationHeader();
46-
}
33+
ServerSettings serverSetting = Settings.GetServerSettings();
4734

48-
return authorizationHeader;
49-
}
35+
string aadTenant = string.Format("{0}/{1}", serverSetting.AzureAuthEndpoint, serverSetting.AADTenant);
36+
string aadResource = serverSetting.Ax7Endpoint;
37+
38+
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(serverSetting.WebAppId)
39+
.WithClientSecret(serverSetting.WebAADKey)
40+
.WithAuthority(new Uri(aadTenant))
41+
.Build();
42+
43+
string[] scopes = new string[] { $"{aadResource}/.default" };
44+
45+
AuthenticationResult result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
46+
authorizationHeader = result.CreateAuthorizationHeader();
47+
return authorizationHeader;
5048
}
5149

5250
/// <summary>
@@ -55,11 +53,13 @@ public static string AuthorizationHeader
5553
/// <returns>AuthenticationHeaderValue object</returns>
5654
public static AuthenticationHeaderValue GetValidAuthenticationHeader(bool _dropAuthHeader = false)
5755
{
58-
if(_dropAuthHeader == true)
56+
if (_dropAuthHeader == true)
5957
{
6058
AuthenticationHelper.DropAuthheader = _dropAuthHeader;
6159
}
62-
return AuthenticationHelper.ParseAuthenticationHeader(AuthenticationHelper.AuthorizationHeader);
60+
Task<string> autHeader = GetAuthorizationHeader();
61+
string header = autHeader.Result;
62+
return AuthenticationHelper.ParseAuthenticationHeader(header);
6363
}
6464

6565
static AuthenticationHeaderValue ParseAuthenticationHeader(string authorizationHeader)

CustomServiceTestUtil/Classes/JSONHttpHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public JSONHttpHelper()
2121
{
2222

2323
}
24-
public void callServiceSync(string _json, DateTime _begin, string _serviceMethod)
24+
public async void callServiceSync(string _json, DateTime _begin, string _serviceMethod)
2525
{
2626
begin = _begin;
2727
serviceMethod = _serviceMethod;
@@ -35,7 +35,7 @@ public void callServiceSync(string _json, DateTime _begin, string _serviceMetho
3535
};
3636

3737
var request = HttpWebRequest.Create(serviceUri.Uri);
38-
request.Headers[OAuthHelper.OAuthHeader] = OAuthHelper.GetAuthenticationHeader();
38+
request.Headers[OAuthHelper.OAuthHeader] = await OAuthHelper.GetAuthenticationHeader();
3939
request.Method = "POST";
4040

4141
using (var stream = request.GetRequestStream())
@@ -162,14 +162,14 @@ private async Task<HttpResponseMessage> callPostJSON(string _json,bool _dropAuth
162162
}
163163
return response;
164164
}
165-
private void postJSONStream(string _json)
165+
private async void postJSONStream(string _json)
166166
{
167167
try
168168
{
169169
ServerSettings serverSettings = Settings.GetServerSettings();
170170
string servicePath = string.Format("{0}{1}{2}", serverSettings.Ax7Endpoint, App.CustomServices, serviceMethod);
171171
var request = HttpWebRequest.Create(servicePath);
172-
request.Headers[OAuthHelper.OAuthHeader] = OAuthHelper.GetAuthenticationHeader();
172+
request.Headers[OAuthHelper.OAuthHeader] = await OAuthHelper.GetAuthenticationHeader();
173173
request.Method = "POST";
174174

175175
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());

CustomServiceTestUtil/Classes/OAuthHelper.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using CustomServiceTestUtil;
2-
using Microsoft.IdentityModel.Clients.ActiveDirectory;
2+
using Microsoft.Identity.Client;
33
using System;
44
using System.Net.Http.Headers;
5+
using System.Threading.Tasks;
56
using System.Windows.Forms;
67

78
namespace AuthenticationUtility
@@ -21,7 +22,9 @@ public class OAuthHelper
2122
/// <returns>AuthenticationHeaderValue object</returns>
2223
public static AuthenticationHeaderValue GetValidAuthenticationHeader()
2324
{
24-
return OAuthHelper.ParseAuthenticationHeader(AuthenticationHelper.AuthorizationHeader);
25+
Task<string> token = AuthenticationHelper.GetAuthorizationHeader();
26+
string header = token.Result;
27+
return OAuthHelper.ParseAuthenticationHeader(header);
2528
}
2629

2730
static AuthenticationHeaderValue ParseAuthenticationHeader(string authorizationHeader)
@@ -37,28 +40,22 @@ static AuthenticationHeaderValue ParseAuthenticationHeader(string authorizationH
3740
/// Retrieves an authentication header from the service.
3841
/// </summary>
3942
/// <returns>The authentication header for the Web API call.</returns>
40-
public static string GetAuthenticationHeader()
43+
public static async Task<string> GetAuthenticationHeader()
4144
{
4245
ServerSettings serverSetting = Settings.GetServerSettings();
4346

4447
string aadTenant = string.Format("{0}/{1}", serverSetting.AzureAuthEndpoint, serverSetting.AADTenant);
4548
string aadResource = serverSetting.Ax7Endpoint;
4649

47-
AuthenticationContext authenticationContext = new AuthenticationContext(aadTenant);
48-
AuthenticationResult authenticationResult = null;
50+
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(serverSetting.WebAppId)
51+
.WithClientSecret(serverSetting.WebAADKey)
52+
.WithAuthority(new Uri(aadTenant))
53+
.Build();
4954

50-
try
51-
{
52-
var credential = new ClientCredential(serverSetting.WebAppId, serverSetting.WebAADKey);
53-
authenticationResult = authenticationContext.AcquireTokenAsync(aadResource, credential).Result;
54-
}
55-
catch (Exception ex)
56-
{
57-
MessageBox.Show(ex.Message.ToString());
58-
}
55+
string[] scopes = new string[] { $"{aadResource}/.default" };
5956

60-
// Create and get JWT token
61-
return authenticationResult.CreateAuthorizationHeader();
57+
AuthenticationResult result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
58+
return result.CreateAuthorizationHeader();
6259
}
6360
}
6461
}

CustomServiceTestUtil/CustomServiceTestUtil.csproj

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,80 @@
5151
<HintPath>packages\MahApps.Metro.1.6.5\lib\net45\MahApps.Metro.dll</HintPath>
5252
<Private>True</Private>
5353
</Reference>
54-
<Reference Include="MahApps.Metro.IconPacks, Version=2.3.0.4, Culture=neutral, processorArchitecture=MSIL">
55-
<HintPath>packages\MahApps.Metro.IconPacks.2.3.0\lib\net45\MahApps.Metro.IconPacks.dll</HintPath>
54+
<Reference Include="MahApps.Metro.IconPacks, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
55+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.dll</HintPath>
5656
</Reference>
57-
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
58-
<HintPath>packages\Microsoft.IdentityModel.Clients.ActiveDirectory.5.2.0\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
57+
<Reference Include="MahApps.Metro.IconPacks.BoxIcons, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
58+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.BoxIcons.dll</HintPath>
59+
</Reference>
60+
<Reference Include="MahApps.Metro.IconPacks.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
61+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.Core.dll</HintPath>
62+
</Reference>
63+
<Reference Include="MahApps.Metro.IconPacks.Entypo, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
64+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.Entypo.dll</HintPath>
65+
</Reference>
66+
<Reference Include="MahApps.Metro.IconPacks.EvaIcons, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
67+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.EvaIcons.dll</HintPath>
68+
</Reference>
69+
<Reference Include="MahApps.Metro.IconPacks.FeatherIcons, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
70+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.FeatherIcons.dll</HintPath>
71+
</Reference>
72+
<Reference Include="MahApps.Metro.IconPacks.FontAwesome, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
73+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.FontAwesome.dll</HintPath>
74+
</Reference>
75+
<Reference Include="MahApps.Metro.IconPacks.Ionicons, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
76+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.Ionicons.dll</HintPath>
77+
</Reference>
78+
<Reference Include="MahApps.Metro.IconPacks.JamIcons, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
79+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.JamIcons.dll</HintPath>
80+
</Reference>
81+
<Reference Include="MahApps.Metro.IconPacks.Material, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
82+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.Material.dll</HintPath>
83+
</Reference>
84+
<Reference Include="MahApps.Metro.IconPacks.MaterialDesign, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
85+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.MaterialDesign.dll</HintPath>
86+
</Reference>
87+
<Reference Include="MahApps.Metro.IconPacks.MaterialLight, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
88+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.MaterialLight.dll</HintPath>
89+
</Reference>
90+
<Reference Include="MahApps.Metro.IconPacks.Microns, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
91+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.Microns.dll</HintPath>
92+
</Reference>
93+
<Reference Include="MahApps.Metro.IconPacks.Modern, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
94+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.Modern.dll</HintPath>
95+
</Reference>
96+
<Reference Include="MahApps.Metro.IconPacks.Octicons, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
97+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.Octicons.dll</HintPath>
98+
</Reference>
99+
<Reference Include="MahApps.Metro.IconPacks.PicolIcons, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
100+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.PicolIcons.dll</HintPath>
101+
</Reference>
102+
<Reference Include="MahApps.Metro.IconPacks.RPGAwesome, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
103+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.RPGAwesome.dll</HintPath>
104+
</Reference>
105+
<Reference Include="MahApps.Metro.IconPacks.SimpleIcons, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
106+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.SimpleIcons.dll</HintPath>
107+
</Reference>
108+
<Reference Include="MahApps.Metro.IconPacks.Typicons, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
109+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.Typicons.dll</HintPath>
110+
</Reference>
111+
<Reference Include="MahApps.Metro.IconPacks.Unicons, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
112+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.Unicons.dll</HintPath>
113+
</Reference>
114+
<Reference Include="MahApps.Metro.IconPacks.WeatherIcons, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
115+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.WeatherIcons.dll</HintPath>
116+
</Reference>
117+
<Reference Include="MahApps.Metro.IconPacks.Zondicons, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0c0d510f9915137a, processorArchitecture=MSIL">
118+
<HintPath>packages\MahApps.Metro.IconPacks.3.6.0\lib\net46\MahApps.Metro.IconPacks.Zondicons.dll</HintPath>
119+
</Reference>
120+
<Reference Include="Microsoft.Identity.Client, Version=4.40.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
121+
<HintPath>packages\Microsoft.Identity.Client.4.40.0\lib\net461\Microsoft.Identity.Client.dll</HintPath>
59122
</Reference>
60123
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
61-
<HintPath>packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
124+
<HintPath>packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
62125
</Reference>
63126
<Reference Include="Newtonsoft.Json.Schema, Version=3.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
64-
<HintPath>packages\Newtonsoft.Json.Schema.3.0.11\lib\net45\Newtonsoft.Json.Schema.dll</HintPath>
127+
<HintPath>packages\Newtonsoft.Json.Schema.3.0.13\lib\net45\Newtonsoft.Json.Schema.dll</HintPath>
65128
</Reference>
66129
<Reference Include="System" />
67130
<Reference Include="System.Data" />

CustomServiceTestUtil/packages.config

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
<packages>
33
<package id="ControlzEx" version="3.0.2.4" targetFramework="net461" />
44
<package id="MahApps.Metro" version="1.6.5" targetFramework="net461" />
5-
<package id="MahApps.Metro.IconPacks" version="2.3.0" targetFramework="net461" />
6-
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="5.2.0" targetFramework="net461" />
7-
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
8-
<package id="Newtonsoft.Json.Schema" version="3.0.11" targetFramework="net461" />
5+
<package id="MahApps.Metro.IconPacks" version="3.6.0" targetFramework="net461" />
6+
<package id="Microsoft.Identity.Client" version="4.40.0" targetFramework="net461" />
7+
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net461" />
8+
<package id="Newtonsoft.Json.Schema" version="3.0.13" targetFramework="net461" />
99
<package id="System.Net.Http" version="4.3.4" targetFramework="net461" />
1010
<package id="System.Security.Cryptography.Algorithms" version="4.3.1" targetFramework="net461" />
1111
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net461" />

0 commit comments

Comments
 (0)