Skip to content

Commit

Permalink
Add azure b2c auth code flow
Browse files Browse the repository at this point in the history
  • Loading branch information
raqeebr-99x committed Aug 23, 2022
1 parent 13f5fc3 commit 4173436
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using TodoApp.Portal.Models;

namespace TodoApp.Portal.Controllers
{
[Authorize]
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TodoApp.Portal.Models
{
public static class AppSettings
{
public static readonly string Identity = "Identity";
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using TodoApp.Portal.Models;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration, AppSettings.Identity);
builder.Services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
options.ResponseType = OpenIdConnectResponseType.Code;
options.Scope.Add(options.ClientId);
});

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages().AddMicrosoftIdentityUI();

var app = builder.Build();

Expand All @@ -17,11 +31,15 @@
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});

app.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Identity.Web" Version="1.25.1" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.25.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!DOCTYPE html>
@using System.Security.Principal
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
Expand All @@ -25,6 +26,12 @@
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
@if (User.Identity?.IsAuthenticated ?? false)
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignOut">Signout</a>
</li>
}
</ul>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@
"Microsoft.AspNetCore": "Warning"
}
},
"Identity": {
"Instance": "https://todoappb2c01.b2clogin.com",
"Domain": "todoappb2c01.onmicrosoft.com",
"ClientId": "7549b5d6-b011-4140-a7f9-5690cbc79157",
"SignUpSignInPolicyId": "B2C_1_signupsignin",
"ResponseType": "code"
},
"AllowedHosts": "*"
}

0 comments on commit 4173436

Please sign in to comment.