Skip to content

Commit

Permalink
Merge pull request #12 from cradle77/features/Preview4
Browse files Browse the repository at this point in the history
Features/preview4
  • Loading branch information
cradle77 authored Dec 8, 2019
2 parents 6c4504b + 852d7ec commit 2254da6
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 22 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

![Nuget (with prereleases)](https://img.shields.io/nuget/vpre/Des.Blazor.Authorization.Msal)

> _.NET Core 3.1 Preview 3 supported_
> _Updated to Blazor .NET Core 3.1 Preview 4_
This NuGet package adds support to Azure Active Directory to your Blazor Web Assembly application. It allows to authenticate the user and then acquire an access token to make a secure call to an external API:

Expand All @@ -24,15 +24,18 @@ The first step is to reference the NuGet package on the *Blazor Client* project:
Install-Package Des.Blazor.Authorization.Msal
```

Then we need to include MSAL.js in *index.html* page, before the reference to blazor.webassembly.js. This could either be a static file we include to our project, or just a link pointing to a CDN URL:
Then we need to include MSAL.js in *index.html* page, before the reference to blazor.webassembly.js *and the script from the component library*. This could either be a static file we include to our project, or just a link pointing to a CDN URL:

```
...
<script src="https://alcdn.msftauth.net/lib/1.1.3/js/msal.js"></script>
<script src="_content/Des.Blazor.Authorization.Msal/js/blazor.azuread.js"></script>
<script src="_framework/blazor.webassembly.js"></script>
</body>
```

> _*Note*: this has changed since Preview2: automatic script embedding doesn't work anymore, please remember to add the script reference manually_
### 2. Register the services and configure the authentication

At this point we are ready to configure the authentication in our *Startup* class, by adding the following lines to the *ConfigureServices* method:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@
<Description>This package is a wrapper over MSAL.js to implement OpenID Connect and OAuth2 on a Blazor WebAssembly application</Description>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="wwwroot\**\*.js" LogicalName="blazor:js:%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
<None Include="wwwroot\js\blazor.azuread.js" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(AssemblyName).Tests</_Parameter1>
Expand All @@ -30,9 +22,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.0-preview3.19555.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.0-preview3.19555.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1.0-preview3.19555.2" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1.0" />
</ItemGroup>

</Project>
6 changes: 5 additions & 1 deletion src/Des.Blazor.Authorization.Msal/LoginModes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
namespace Des.Blazor.Authorization.Msal
using System.Text.Json.Serialization;

namespace Des.Blazor.Authorization.Msal
{
[JsonConverter(typeof(JsonStringEnumConverter))]

public enum LoginModes
{
Popup,
Expand Down
4 changes: 2 additions & 2 deletions src/Sample/Blazor.Msal.Api/Blazor.Msal.Api.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
Expand All @@ -9,7 +9,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.1.0-preview3.19555.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.1.0" />
</ItemGroup>

</Project>
10 changes: 5 additions & 5 deletions src/Sample/Blazor.Msal.Client/Blazor.Msal.Client.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.1.0-preview3.19555.2" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.1.0-preview3.19555.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.1.0-preview3.19555.2" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="3.1.0-preview3.19555.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.1.0-preview4.19579.2" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.1.0-preview4.19579.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.1.0-preview4.19579.2" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="3.1.0-preview4.19579.2" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/Sample/Blazor.Msal.Client/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>

<script src="https://alcdn.msftauth.net/lib/1.1.3/js/msal.js"></script>
<script src="_content/Des.Blazor.Authorization.Msal/js/blazor.azuread.js"></script>
<script src="_framework/blazor.webassembly.js"></script>
</body>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,53 @@ public async Task AddAzureActiveDirectory_WithUri_FetchesConfigFromUri()
Assert.Equal(config.Authority, result.Authority);
Assert.Equal(config.LoginMode, result.LoginMode);
}

[Fact]
public async Task AddAzureActiveDirectory_WithUri_CanDeserializeEnum()
{
var services = new ServiceCollection();

var jsRuntime = new Mock<IJSRuntime>();
services.AddTransient(sp => jsRuntime.Object);

var config = new TestConfig()
{
LoginMode = LoginModes.Redirect
};

var json =
@"{
""Authority"": ""https://myauthority.com/"",
""ClientId"": ""12345"",
""LoginMode"": ""Redirect""
}";

var httpResponse = new HttpResponseMessage()
{
Content = new StringContent(json)
};

var httpMessageHandler = new Mock<HttpMessageHandler>();
httpMessageHandler
.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.Returns(Task.FromResult(httpResponse));

services.AddTransient(sp => new HttpClient(httpMessageHandler.Object));

var navigationManager = new TestNavigationManager();
services.AddTransient<NavigationManager>(sp => navigationManager);

services.AddAzureActiveDirectory(new Uri("config/config.json", UriKind.Relative));

var provider = services.BuildServiceProvider();
var configurator = provider.GetService<IConfigProvider<IMsalConfig>>();

var result = await configurator.GetConfigurationAsync();

Assert.Equal(config.ClientId, result.ClientId);
Assert.Equal(config.Authority, result.Authority);
Assert.Equal(config.LoginMode, result.LoginMode);
}
}
}

0 comments on commit 2254da6

Please sign in to comment.