Skip to content

Commit a19ecbe

Browse files
authored
Merge pull request #63 from microsoft/Nava_updates
Updating sample
2 parents a50b7aa + 5e64b54 commit a19ecbe

File tree

4 files changed

+342
-9
lines changed

4 files changed

+342
-9
lines changed
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
4+
<connectionStrings>
5+
6+
7+
8+
<!--
9+
10+
To run all samples without being prompted for login credentials, un-comment and edit one of the example connection strings below using appropriate values for your environment.
11+
12+
Otherwise, you will be prompted for login information when you run a sample project.
13+
14+
15+
16+
Use the system.diagnostics/switches keys below to control levels of logging.
17+
18+
-->
19+
20+
21+
22+
<!--
23+
24+
** !IMPORTANT! **
25+
26+
27+
28+
Be mindful that the connection information you enter in these will be available to anyone with access to this file.
29+
30+
Do not push changes to this file to a cloned GitHub repository.
31+
32+
33+
-->
34+
35+
<!--Online using Office 365-->
36+
37+
<!--<add name="Connect" connectionString="Url=https://yourorgname.crm.dynamics.com; [email protected]; Password=password; authtype=Office365; RequireNewInstance=True"/>-->
38+
39+
40+
</connectionStrings>
41+
42+
<system.diagnostics>
43+
44+
<trace autoflush="true"/>
45+
46+
<sources>
47+
48+
<source name="Microsoft.Xrm.Tooling.Connector.CrmServiceClient" switchName="Microsoft.Xrm.Tooling.Connector.CrmServiceClient" switchType="System.Diagnostics.SourceSwitch">
49+
50+
<listeners>
51+
52+
<add name="console" type="System.Diagnostics.ConsoleTraceListener"/>
53+
54+
<add name="fileListener"/>
55+
56+
</listeners>
57+
58+
</source>
59+
60+
<source name="Microsoft.Xrm.Tooling.CrmConnectControl" switchName="Microsoft.Xrm.Tooling.CrmConnectControl" switchType="System.Diagnostics.SourceSwitch">
61+
62+
<listeners>
63+
64+
<add name="console" type="System.Diagnostics.ConsoleTraceListener"/>
65+
66+
<add name="fileListener"/>
67+
68+
</listeners>
69+
70+
</source>
71+
72+
<!--ADALListener conflicts with using ADAL libraries directly without the Xrm.Tooling.Connector assemblies.
73+
74+
Uncomment this if you are using the Xrm.Tooling.Connector-->
75+
76+
<!--<source name="Microsoft.IdentityModel.Clients.ActiveDirectory" switchName="Microsoft.IdentityModel.Clients.ActiveDirectory" switchType="System.Diagnostics.SourceSwitch">
77+
78+
<listeners>
79+
80+
<add name="console" type="System.Diagnostics.ConsoleTraceListener"/>
81+
82+
<remove name="Default"/>
83+
84+
<add name="ADALListener"/>
85+
86+
</listeners>
87+
88+
</source>-->
89+
90+
</sources>
91+
92+
<switches>
93+
94+
<!--
95+
96+
Possible values for switches: Off, Error, Warning, Info, Verbose
97+
98+
All" includes Error, Warning, Info, Verbose, Activity Tracing Trace levels
99+
100+
ActivityTracing: Just Activity Tracing
101+
102+
Verbose: includes Error, Warning, Info, Trace levels
103+
104+
Info: includes Error, Warning, Info levels
105+
106+
Warning: includes Error, Warning levels
107+
108+
Error: includes Error level
109+
110+
-->
111+
112+
<add name="Microsoft.Xrm.Tooling.Connector.CrmServiceClient" value="Error"/>
113+
114+
<add name="Microsoft.Xrm.Tooling.CrmConnectControl" value="Error"/>
115+
116+
<add name="Microsoft.IdentityModel.Clients.ActiveDirectory" value="Error"/>
117+
118+
</switches>
119+
120+
<sharedListeners>
121+
<add name="fileListener" type="Microsoft.Xrm.Tooling.Connector.DynamicsFileLogTraceListener, Microsoft.Xrm.Tooling.Connector"/>
122+
123+
<add name="fileListener" type="Microsoft.Xrm.Tooling.Connector.DynamicsFileLogTraceListener, Microsoft.Xrm.Tooling.Connector" BaseFileName="PowerApps-Sample-Log" Location="LocalUserApplicationDirectory" MaxFileSize="52428800"/>
124+
125+
<!--
126+
127+
fileListener Log written to
128+
129+
\\{Computer Name}\Users\{UserName}\AppData\Roaming\PowerApps\{Sample Project Name}\1.0.0.0\PowerApps-Sample-Log-{YYYY}-{MM}-{DD}.log
130+
131+
-->
132+
133+
<!--ADALListener conflicts with using ADAL libraries directly without the Xrm.Tooling.Connector assemblies.
134+
135+
Uncomment this if you are using the Xrm.Tooling.Connector-->
136+
137+
<!--<add name="ADALListener"
138+
139+
type="Microsoft.Xrm.Tooling.Connector.DynamicsFileLogTraceListener, Microsoft.Xrm.Tooling.Connector"
140+
141+
BaseFileName="PowerApps-Sample-Log-ADAL"
142+
143+
Location="LocalUserApplicationDirectory"
144+
145+
MaxFileSize="52428800"/>-->
146+
147+
<!--
148+
149+
ADALListener Log written to
150+
151+
\\{Computer Name}\Users\{UserName}\AppData\Roaming\PowerApps\{Sample Project Name}\1.0.0.0\PowerApps-Sample-Log-ADAL-{YYYY}-{MM}-{DD}.log
152+
153+
-->
154+
155+
</sharedListeners>
156+
157+
</system.diagnostics>
158+
159+
160+
161+
<startup>
162+
163+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
164+
165+
</startup>
166+
167+
<runtime>
168+
169+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
170+
171+
<dependentAssembly>
172+
173+
<assemblyIdentity name="Microsoft.Xrm.Sdk" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
174+
175+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0"/>
176+
177+
</dependentAssembly>
178+
179+
<dependentAssembly>
180+
181+
<assemblyIdentity name="Microsoft.Xrm.Sdk.Workflow" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
182+
183+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0"/>
184+
185+
</dependentAssembly>
186+
187+
<dependentAssembly>
188+
189+
<assemblyIdentity name="Microsoft.Xrm.Tooling.Connector" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
190+
191+
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
192+
193+
</dependentAssembly>
194+
195+
<dependentAssembly>
196+
197+
<assemblyIdentity name="Microsoft.Crm.Sdk.Proxy" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
198+
199+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0"/>
200+
201+
</dependentAssembly>
202+
203+
</assemblyBinding>
204+
205+
</runtime>
206+
207+
</configuration>

cds/webapi/C#/BasicOperations/BasicOperations/BasicOperations.csproj

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35-
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
36-
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.5.2.3\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
35+
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.29.0.1078, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
36+
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.29.0\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
37+
</Reference>
38+
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms, Version=2.29.0.1078, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
39+
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.29.0\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll</HintPath>
3740
</Reference>
3841
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
3942
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
@@ -66,17 +69,13 @@
6669
<Reference Include="System.Xml" />
6770
</ItemGroup>
6871
<ItemGroup>
69-
<Compile Include="..\..\SampleHelpers.cs">
70-
<Link>SampleHelpers.cs</Link>
71-
</Compile>
72+
<Compile Include="SampleHelpers.cs" />
7273
<Compile Include="SampleMethod.cs" />
7374
<Compile Include="SampleProgram.cs" />
7475
<Compile Include="Properties\AssemblyInfo.cs" />
7576
</ItemGroup>
7677
<ItemGroup>
77-
<None Include="..\..\..\..\App.config">
78-
<Link>App.config</Link>
79-
</None>
78+
<None Include="App.config" />
8079
<None Include="packages.config" />
8180
</ItemGroup>
8281
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using Microsoft.IdentityModel.Clients.ActiveDirectory;
2+
using System;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Net.Http;
6+
using System.Net.Http.Headers;
7+
using System.Threading.Tasks;
8+
9+
namespace PowerApps.Samples
10+
{
11+
public class SampleHelpers
12+
{
13+
//These sample application registration values are available for all online instances.
14+
public static string clientId = "51f81489-12ee-4a9e-aaae-a2591f45987d";
15+
public static string redirectUrl = "app://58145B91-0C36-4500-8554-080854F2AC97";
16+
17+
18+
public static string GetParameterValueFromConnectionString(string connectionString, string parameter)
19+
{
20+
try
21+
{
22+
return connectionString.Split(';').Where(s => s.Trim().StartsWith(parameter)).FirstOrDefault().Split('=')[1];
23+
}
24+
catch (Exception)
25+
{
26+
return string.Empty;
27+
}
28+
29+
}
30+
31+
public static HttpClient GetHttpClient(string connectionString, string clientId, string redirectUrl, string version)
32+
{
33+
string url = GetParameterValueFromConnectionString(connectionString, "Url");
34+
string username = GetParameterValueFromConnectionString(connectionString, "Username");
35+
string domain = GetParameterValueFromConnectionString(connectionString, "Domain");
36+
string password = GetParameterValueFromConnectionString(connectionString, "Password");
37+
string authType = GetParameterValueFromConnectionString(connectionString, "authtype");
38+
try
39+
{
40+
HttpMessageHandler messageHandler;
41+
42+
switch (authType)
43+
{
44+
case "Office365":
45+
case "IFD":
46+
47+
messageHandler = new OAuthMessageHandler(url, clientId, redirectUrl, username, password,
48+
new HttpClientHandler());
49+
break;
50+
case "AD":
51+
NetworkCredential credentials = new NetworkCredential(username, password, domain);
52+
messageHandler = new HttpClientHandler() { Credentials = credentials };
53+
break;
54+
default:
55+
throw new ArgumentOutOfRangeException("Valid authType values are 'Office365', 'IFD', or 'AD'.");
56+
57+
}
58+
59+
HttpClient httpClient = new HttpClient(messageHandler)
60+
{
61+
BaseAddress = new Uri(string.Format("{0}/api/data/{1}/", url, version)),
62+
63+
Timeout = new TimeSpan(0, 2, 0) //2 minutes
64+
};
65+
66+
return httpClient;
67+
}
68+
catch (Exception)
69+
{
70+
71+
throw;
72+
}
73+
}
74+
75+
/// <summary> Displays exception information to the console. </summary>
76+
/// <param name="ex">The exception to output</param>
77+
public static void DisplayException(Exception ex)
78+
{
79+
Console.WriteLine("The application terminated with an error.");
80+
Console.WriteLine(ex.Message);
81+
while (ex.InnerException != null)
82+
{
83+
Console.WriteLine("\t* {0}", ex.InnerException.Message);
84+
ex = ex.InnerException;
85+
}
86+
}
87+
}
88+
89+
/// <summary>
90+
///Custom HTTP message handler that uses OAuth authentication thru ADAL.
91+
/// </summary>
92+
class OAuthMessageHandler : DelegatingHandler
93+
{
94+
private AuthenticationHeaderValue authHeader;
95+
96+
public OAuthMessageHandler(string serviceUrl, string clientId, string redirectUrl, string username, string password,
97+
HttpMessageHandler innerHandler)
98+
: base(innerHandler)
99+
{
100+
// Obtain the Azure Active Directory Authentication Library (ADAL) authentication context.
101+
AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(
102+
new Uri(serviceUrl + "/api/data/")).Result;
103+
AuthenticationContext authContext = new AuthenticationContext(ap.Authority, false);
104+
//Note that an Azure AD access token has finite lifetime, default expiration is 60 minutes.
105+
AuthenticationResult authResult;
106+
if (username != string.Empty && password != string.Empty)
107+
{
108+
109+
UserCredential cred = new UserCredential(username, password);
110+
authResult = authContext.AcquireToken(serviceUrl, clientId, cred);
111+
}
112+
else
113+
{
114+
authResult = authContext.AcquireToken(serviceUrl, clientId, new Uri(redirectUrl), PromptBehavior.Auto);
115+
}
116+
117+
authHeader = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
118+
}
119+
120+
protected override Task<HttpResponseMessage> SendAsync(
121+
HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
122+
{
123+
request.Headers.Authorization = authHeader;
124+
return base.SendAsync(request, cancellationToken);
125+
}
126+
}
127+
}

cds/webapi/C#/BasicOperations/BasicOperations/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="5.2.3" targetFramework="net461" />
3+
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.29.0" targetFramework="net461" />
44
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
55
<package id="System.Net.Http" version="4.3.4" targetFramework="net461" />
66
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net461" />

0 commit comments

Comments
 (0)