Skip to content

Commit

Permalink
Fix HTTP/HTTPS signout issue
Browse files Browse the repository at this point in the history
- Apply redirect config as added by AzureAD/microsoft-identity-web#115
- Add Signout redirect
- Add Application Insights
- Covers issue #1 and even already some role-based experiments
  • Loading branch information
markusrt committed Jun 8, 2020
1 parent 51504fe commit a79fd84
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 38 deletions.
18 changes: 4 additions & 14 deletions NRZMyk.Components/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,12 @@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
@*<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />*@
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>

@* <Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<CascadingAuthenticationState>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router> *@
</CascadingAuthenticationState>
</NotFound>
</Router>
2 changes: 2 additions & 0 deletions NRZMyk.Components/Pages/Counter.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@

<h1>Counter</h1>
<p>Current count: @CurrentCount</p>
<p>Groups: @Groups</p>
<p>Roles: @Roles</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
24 changes: 22 additions & 2 deletions NRZMyk.Components/Pages/CounterBase.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
using Microsoft.AspNetCore.Components;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;

namespace NRZMyk.Components.Pages
{
public class CounterBase : ComponentBase
{
[Inject]
public AuthenticationStateProvider AuthenticationStateProvider { get; set; }

public int CurrentCount { get; set; }

public void IncrementCount()
public string Groups { get; set; }

public string Roles { get; set; }

protected override async Task OnInitializedAsync()
{
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = state.User;
var claims = user.Claims;
Groups = string.Join(",", claims.Where(c => c.Type == "groups").Select(c => c.Value));
Roles = string.Join(",", claims.Where(c => c.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role").Select(c => c.Value));
await base.OnInitializedAsync();
}

public async Task IncrementCount()
{
CurrentCount++;
}
Expand Down
5 changes: 5 additions & 0 deletions NRZMyk.Components/Pages/Writer.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@page "/writer"
@attribute [Authorize(Roles = "Writer")]

<h1>Writer</h1>
<p>Access allowed for writer role only</p>
6 changes: 3 additions & 3 deletions NRZMyk.Components/Shared/LoginDisplay.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<AuthorizeView>
<Authorized>
Hello, @context.User.Identity.Name!
<a href="AzureAD/Account/SignOut">Log out</a>
<a href="MicrosoftIdentity/Account/SignOut">Log out</a>
</Authorized>
<NotAuthorized>
<a href="AzureAD/Account/SignIn">Log in</a>
<a href="MicrosoftIdentity/Account/SignIn">Log in</a>
</NotAuthorized>
</AuthorizeView>
</AuthorizeView>
12 changes: 12 additions & 0 deletions NRZMyk.Components/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,17 @@
<span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
</NavLink>
</li>

</ul>
<AuthorizeView Roles="Writer">
<Authorized>
<ul class="nav flex-column">
<li class="nav-item px-3">
<NavLink class="nav-link" href="writer">
<span class="oi oi-list-rich" aria-hidden="true"></span> Writer access
</NavLink>
</li>
</ul>
</Authorized>
</AuthorizeView>
</div>
7 changes: 0 additions & 7 deletions NRZMyk.Server/Areas/AzureAD/Pages/Account/SignedOut.cshtml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@page
@functions {
public async Task<IActionResult> OnGet()
{
return Redirect("~/");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace NRZMyk.Server.Areas.MicrosoftIdentity.Account
{
public class SignedOutModel : PageModel
{
public void OnGet()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ProviderId": "Microsoft.ApplicationInsights.ConnectedService.ConnectedServiceProvider",
"Version": "16.0.0.0"
}
10 changes: 8 additions & 2 deletions NRZMyk.Server/NRZMyk.Server.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 @@ -7,10 +7,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.1.4" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.13.1" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.1" />
<PackageReference Include="Microsoft.Identity.Web" Version="0.1.4-preview" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="0.1.4-preview" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.9.10" />
</ItemGroup>

Expand All @@ -19,4 +21,8 @@
<ProjectReference Include="..\NRZMyk.Services\NRZMyk.Services.csproj" />
</ItemGroup>

<ItemGroup>
<WCFMetadata Include="Connected Services" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions NRZMyk.Server/Properties/serviceDependencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": {
"appInsights1": {
"type": "appInsights",
"connectionId": null
}
}
}
8 changes: 8 additions & 0 deletions NRZMyk.Server/Properties/serviceDependencies.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": {
"appInsights1": {
"type": "appInsights.sdk",
"connectionId": null
}
}
}
13 changes: 8 additions & 5 deletions NRZMyk.Server/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.AzureAD.UI;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components.Authorization;
Expand All @@ -11,6 +11,8 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using NRZMyk.Services.Data;
using NRZMyk.Services.Service;

Expand All @@ -29,8 +31,7 @@ public Startup(IConfiguration configuration)
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
services.AddSignIn(Configuration);

services.AddControllersWithViews(options =>
{
Expand All @@ -40,16 +41,18 @@ public void ConfigureServices(IServiceCollection services)
options.Filters.Add(new AuthorizeFilter(policy));
});


services.AddMvc().AddRazorPagesOptions(options => { options.RootDirectory = "/"; });
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddRazorPages();
services.AddRazorPages().AddMicrosoftIdentityUI();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();

services.AddApplicationInsightsTelemetry();
}


// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
Expand Down
9 changes: 5 additions & 4 deletions NRZMyk.Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
},
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "",
"TenantId": "",
"ClientId": "",
"CallbackPath": "/signin-oidc"
"Domain": "[Enter the domain of your tenant, e.g. contoso.onmicrosoft.com]",
"TenantId": "[Enter 'common', or 'organizations' or the Tenant Id (Obtained from the Azure portal. Select 'Endpoints' from the 'App registrations' blade and use the GUID in any of the URLs), e.g. da41245a5-11b3-996c-00a8-4d99re19f292]",
"ClientId": "[Enter the Client Id (Application ID obtained from the Azure portal), e.g. ba74781c2-53c2-442a-97c2-3d60re42f403]",
"CallbackPath": "/signin-oidc",
"SignedOutCallbackPath ": "/signout-callback-oidc"
},
"AllowedHosts": "*"
}
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ This app authenticates with Azure AD. In order to run it locally you need to con

- `AzureAd__Domain`: Your domain
- `AzureAd__TenantId`: Your tenant ID
- `AzureAd__ClientId`: Your apps client ID
- `AzureAd__ClientId`: Your apps client ID

## Deployment setup

### Fix Azure AD redirect issues

There are cases where RedirectUri is needed, for instance when you use a reverse proxy that transforms HTTPS
URLs (external world) to HTTP URLs (inside the protected area). This can also be useful for Web apps running
in containers (for the same reasons). I.e. in case that AAD tries to redirect to <http://your.host.name/signin-oidc>
you would need to override the following config value:

- `AzureAd__RedirectUri`: `<https://your.host.name/signin-oidc>`

0 comments on commit a79fd84

Please sign in to comment.