Skip to content

Commit

Permalink
code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbod committed Apr 26, 2024
1 parent d0a15fa commit 57277ce
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 17 deletions.
6 changes: 3 additions & 3 deletions server/BffMicrosoftEntraID.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.4" NoWarn="NU1605" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.4" />
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.17.4" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.17.4" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.17.4" />
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.18.1" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.18.1" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.18.1" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="0.21.0" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders.TagHelpers" Version="0.21.0" />
<PackageReference Include="Yarp.ReverseProxy" Version="2.1.0" />
Expand Down
3 changes: 1 addition & 2 deletions server/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public ActionResult Login(string? returnUrl, string? claimsChallenge)

if (claimsChallenge != null)
{
string jsonString = claimsChallenge.Replace("\\", "")
.Trim(['"']);
string jsonString = claimsChallenge.Replace("\\", "").Trim('"');

properties.Items["claims"] = jsonString;
}
Expand Down
9 changes: 5 additions & 4 deletions server/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ public class UserController : ControllerBase
public IActionResult GetCurrentUser() => Ok(CreateUserInfo(User));

private static UserInfo CreateUserInfo(ClaimsPrincipal claimsPrincipal)
{
if (!claimsPrincipal?.Identity?.IsAuthenticated ?? true)
{
if (claimsPrincipal == null || claimsPrincipal.Identity == null
|| !claimsPrincipal.Identity.IsAuthenticated)
{
return UserInfo.Anonymous;
}
Expand All @@ -20,7 +21,7 @@ private static UserInfo CreateUserInfo(ClaimsPrincipal claimsPrincipal)
IsAuthenticated = true
};

if (claimsPrincipal?.Identity is ClaimsIdentity claimsIdentity)
if (claimsPrincipal.Identity is ClaimsIdentity claimsIdentity)
{
userInfo.NameClaimType = claimsIdentity.NameClaimType;
userInfo.RoleClaimType = claimsIdentity.RoleClaimType;
Expand All @@ -31,7 +32,7 @@ private static UserInfo CreateUserInfo(ClaimsPrincipal claimsPrincipal)
userInfo.RoleClaimType = ClaimTypes.Role;
}

if (claimsPrincipal?.Claims?.Any() ?? false)
if (claimsPrincipal.Claims?.Any() ?? false)
{
// Add just the name claim
var claims = claimsPrincipal.FindAll(userInfo.NameClaimType)
Expand Down
2 changes: 1 addition & 1 deletion server/Pages/Error.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@model BffMicrosoftEntraID.Server.Pages.ErrorModel

<!DOCTYPE html>
<html>
<html lang="en">

<head>
<meta charset="utf-8" />
Expand Down
1 change: 0 additions & 1 deletion server/Pages/Error.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace BffMicrosoftEntraID.Server.Pages;

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
public string? RequestId { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

services.AddMicrosoftIdentityWebAppAuthentication(configuration, "MicrosoftEntraID")
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph("https://graph.microsoft.com/v1.0", initialScopes)
.AddMicrosoftGraph(defaultScopes: initialScopes)
.AddInMemoryTokenCaches();

// If using downstream APIs and in memory cache, you need to reset the cookie session if the cache is missing
Expand Down
2 changes: 1 addition & 1 deletion server/RejectSessionCookieWhenAccountNotInCacheEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async override Task ValidatePrincipal(CookieValidatePrincipalContext cont
var tokenAcquisition = context.HttpContext.RequestServices
.GetRequiredService<ITokenAcquisition>();

string token = await tokenAcquisition.GetAccessTokenForUserAsync(scopes: _downstreamScopes,
await tokenAcquisition.GetAccessTokenForUserAsync(scopes: _downstreamScopes,
user: context.Principal);
}
catch (MicrosoftIdentityWebChallengeUserException ex) when (AccountDoesNotExitInTokenCache(ex))
Expand Down
5 changes: 1 addition & 4 deletions server/SecurityHeadersDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ public static class SecurityHeadersDefinitions
{
public static HeaderPolicyCollection GetHeaderPolicyCollection(bool isDev, string? idpHost)
{
if(idpHost == null)
{
throw new ArgumentNullException(nameof(idpHost));
}
ArgumentNullException.ThrowIfNull(idpHost);

var policy = new HeaderPolicyCollection()
.AddFrameOptionsDeny()
Expand Down

0 comments on commit 57277ce

Please sign in to comment.