Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Workaround Microsoft.Graph for local iOS build #17947

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Graph">
<PackageReference Include="Microsoft.Graph" Condition="'$(Configuration)'!='Debug' or $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))!='ios'"> <!-- Workaround for https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/2617 -->
<Version>5.56.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Identity.Client">
Expand Down
24 changes: 21 additions & 3 deletions src/SamplesApp/UITests.Shared/Msal/MsalLoginAndGraph.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
#if !DEBUG || !__IOS__ // Workaround for https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/2617
#define DISABLE_GRAPH
#endif
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Graph;
using Microsoft.Identity.Client;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Authentication;
Expand All @@ -15,12 +17,19 @@
using Uno.UI.MSAL;
using Uno.UI.Samples.Controls;
using Prompt = Microsoft.Identity.Client.Prompt;
#if !DISABLE_GRAPH
using Microsoft.Graph;
#endif

namespace UITests.Msal
{
[Sample("MSAL", IgnoreInSnapshotTests = true)]
public sealed partial class MsalLoginAndGraph : Page, IAuthenticationProvider
public sealed partial class MsalLoginAndGraph : Page
#if !DISABLE_GRAPH
, IAuthenticationProvider
#endif
{
#if !DISABLE_GRAPH
private const string CLIENT_ID = "a74f513b-2d8c-45c0-a15a-15e63f7a7862";
private const string TENANT_ID = "6d53ef61-b6d1-4150-ae0b-43b90e75e0cd";

Expand All @@ -37,31 +46,37 @@ public sealed partial class MsalLoginAndGraph : Page, IAuthenticationProvider
private readonly string[] SCOPES = new[] { "https://graph.microsoft.com/User.Read", "https://graph.microsoft.com/email", "https://graph.microsoft.com/profile" };

private readonly IPublicClientApplication _app;
#endif

public MsalLoginAndGraph()
{
this.InitializeComponent();

#if !DISABLE_GRAPH
_app = PublicClientApplicationBuilder
.Create(CLIENT_ID)
.WithTenantId(TENANT_ID)
.WithRedirectUri(REDIRECT_URI)
.WithUnoHelpers()
.Build();
#endif
}

private async void SignIn(object sender, RoutedEventArgs e)
{
#if !DISABLE_GRAPH
var result = await _app.AcquireTokenInteractive(SCOPES)
.WithPrompt(Prompt.SelectAccount)
.WithUnoHelpers()
.ExecuteAsync();

tokenBox.Text = result.AccessToken;
#endif
}

private async void LoadFromGraph(object sender, RoutedEventArgs e)
{
#if !DISABLE_GRAPH
var httpClient = new HttpClient();
var client = new GraphServiceClient(httpClient, this);

Expand Down Expand Up @@ -89,12 +104,15 @@ private async void LoadFromGraph(object sender, RoutedEventArgs e)
{
Console.Error.WriteLine(exception);
}
#endif
}

#if !DISABLE_GRAPH
Task IAuthenticationProvider.AuthenticateRequestAsync(RequestInformation request, Dictionary<string, object> additionalAuthenticationContext, CancellationToken cancellationToken)
{
request.Headers.Add("Authorization", $"Bearer {tokenBox.Text}");
return Task.CompletedTask;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Microsoft.Graph;
using Uno.UI.Samples.Controls;
using Windows.Foundation;
using Windows.Foundation.Collections;
Expand Down
Loading