From f019c83645261bffac521e0890f052665beb862b Mon Sep 17 00:00:00 2001 From: Jessy Kyalo Musyimi Date: Fri, 21 May 2021 10:15:39 +0200 Subject: [PATCH 1/2] [major] upgrade to Net.5.0 --- ...00000000000_CreateIdentitySchema.Designer.cs | 6 +++--- .../ApplicationDbContextModelSnapshot.cs | 6 +++--- Source/DemoWebApp/DemoWebApp.csproj | 12 ++++++------ .../Authentication/Saml2Handler.cs | 17 ++++++++--------- .../Bindings/HttpArtifactBinding.cs | 2 -- .../Bindings/HttpRedirectBinding.cs | 15 +++------------ .../Configuration/Saml2PostConfigureOptions.cs | 2 +- .../Saml2.Authentication.Core.csproj | 6 +++--- 8 files changed, 27 insertions(+), 39 deletions(-) diff --git a/Source/DemoWebApp/Data/Migrations/00000000000000_CreateIdentitySchema.Designer.cs b/Source/DemoWebApp/Data/Migrations/00000000000000_CreateIdentitySchema.Designer.cs index d3740f7..385bc06 100644 --- a/Source/DemoWebApp/Data/Migrations/00000000000000_CreateIdentitySchema.Designer.cs +++ b/Source/DemoWebApp/Data/Migrations/00000000000000_CreateIdentitySchema.Designer.cs @@ -35,7 +35,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasKey("Id"); b.HasIndex("NormalizedName") - .HasName("RoleNameIndex"); + .HasDatabaseName("RoleNameIndex"); b.ToTable("AspNetRoles"); }); @@ -166,11 +166,11 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasKey("Id"); b.HasIndex("NormalizedEmail") - .HasName("EmailIndex"); + .HasDatabaseName("EmailIndex"); b.HasIndex("NormalizedUserName") .IsUnique() - .HasName("UserNameIndex"); + .HasDatabaseName("UserNameIndex"); b.ToTable("AspNetUsers"); }); diff --git a/Source/DemoWebApp/Data/Migrations/ApplicationDbContextModelSnapshot.cs b/Source/DemoWebApp/Data/Migrations/ApplicationDbContextModelSnapshot.cs index aa8f6f1..51c66ba 100644 --- a/Source/DemoWebApp/Data/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/Source/DemoWebApp/Data/Migrations/ApplicationDbContextModelSnapshot.cs @@ -34,7 +34,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("Id"); b.HasIndex("NormalizedName") - .HasName("RoleNameIndex"); + .HasDatabaseName("RoleNameIndex"); b.ToTable("AspNetRoles"); }); @@ -165,11 +165,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("Id"); b.HasIndex("NormalizedEmail") - .HasName("EmailIndex"); + .HasDatabaseName("EmailIndex"); b.HasIndex("NormalizedUserName") .IsUnique() - .HasName("UserNameIndex"); + .HasDatabaseName("UserNameIndex"); b.ToTable("AspNetUsers"); }); diff --git a/Source/DemoWebApp/DemoWebApp.csproj b/Source/DemoWebApp/DemoWebApp.csproj index 601abf3..73dd7ea 100644 --- a/Source/DemoWebApp/DemoWebApp.csproj +++ b/Source/DemoWebApp/DemoWebApp.csproj @@ -1,16 +1,16 @@  - netcoreapp3.1 + net5.0 aspnet-DemoWebApp-C260B8F3-E5A3-4A4D-B57A-771F079933AA - - - - - + + + + + diff --git a/Source/Saml2.Authentication.Core/Authentication/Saml2Handler.cs b/Source/Saml2.Authentication.Core/Authentication/Saml2Handler.cs index bb945c2..3d3576a 100644 --- a/Source/Saml2.Authentication.Core/Authentication/Saml2Handler.cs +++ b/Source/Saml2.Authentication.Core/Authentication/Saml2Handler.cs @@ -70,7 +70,7 @@ public async Task SignOutAsync(AuthenticationProperties properties) _logger.LogDebug($"Entering {nameof(SignOutAsync)}", properties); var logoutRequestId = CreateUniqueId(); - properties = properties ?? new AuthenticationProperties(); + properties ??= new AuthenticationProperties(); properties.Items.Add(LogoutRequestIdKey, logoutRequestId); properties.Items.Add(nameof(Options.SignOutScheme), Options.SignOutScheme); @@ -88,7 +88,7 @@ protected override async Task HandleChallengeAsync(AuthenticationProperties prop { _logger.LogDebug($"Entering {nameof(HandleChallengeAsync)}", properties); - properties = properties ?? new AuthenticationProperties(); + properties ??= new AuthenticationProperties(); var authnRequestId = CreateUniqueId(); properties.Items.Add(AuthnRequestIdKey, authnRequestId); @@ -103,9 +103,9 @@ protected override async Task HandleChallengeAsync(AuthenticationProperties prop private async Task HandleSignOut() { - if (!Request.Path.Value.EndsWith(ServiceProviderConfiguration.SingleLogoutServiceUrl, StringComparison.OrdinalIgnoreCase) - || !Request.Path.Value.EndsWith(ServiceProviderConfiguration.SingleLogoutResponseServiceUrl, StringComparison.OrdinalIgnoreCase) - || !_httpRedirectBinding.IsValid()) + if (Request.Path.Value != null && (!Request.Path.Value.EndsWith(ServiceProviderConfiguration.SingleLogoutServiceUrl, StringComparison.OrdinalIgnoreCase) + || !Request.Path.Value.EndsWith(ServiceProviderConfiguration.SingleLogoutResponseServiceUrl, StringComparison.OrdinalIgnoreCase) + || !_httpRedirectBinding.IsValid())) { return false; } @@ -146,8 +146,7 @@ private async Task HandleSignOut() private async Task HandleSignIn() { - if (!Request.Path.Value.EndsWith(ServiceProviderConfiguration.AssertionConsumerServiceUrl, StringComparison.OrdinalIgnoreCase) - || !_httpRedirectBinding.IsValid()) + if (Request.Path.Value != null && (!Request.Path.Value.EndsWith(ServiceProviderConfiguration.AssertionConsumerServiceUrl, StringComparison.OrdinalIgnoreCase) || !_httpRedirectBinding.IsValid())) { return false; } @@ -172,8 +171,8 @@ private async Task HandleSignIn() private async Task HandleHttpArtifact(string providerName) { - if (!Request.Path.Value.EndsWith(ServiceProviderConfiguration.AssertionConsumerServiceUrl, StringComparison.OrdinalIgnoreCase) - || !_httpArtifactBinding.IsValid()) + if (Request.Path.Value != null && (!Request.Path.Value.EndsWith(ServiceProviderConfiguration.AssertionConsumerServiceUrl, StringComparison.OrdinalIgnoreCase) + || !_httpArtifactBinding.IsValid())) { return false; } diff --git a/Source/Saml2.Authentication.Core/Bindings/HttpArtifactBinding.cs b/Source/Saml2.Authentication.Core/Bindings/HttpArtifactBinding.cs index bf362a9..a0491eb 100644 --- a/Source/Saml2.Authentication.Core/Bindings/HttpArtifactBinding.cs +++ b/Source/Saml2.Authentication.Core/Bindings/HttpArtifactBinding.cs @@ -3,7 +3,6 @@ using System; using System.IO; using System.Xml; - using Configuration; using dk.nita.saml20.Utils; using Extensions; using Microsoft.AspNetCore.Http; @@ -20,7 +19,6 @@ internal class HttpArtifactBinding : HttpSoapBinding, IHttpArtifactBinding public HttpArtifactBinding( IHttpContextAccessor httpContextAccessor, - Saml2Configuration configuration, IConfigurationProvider configurationProvider) { _httpContextAccessor = httpContextAccessor; diff --git a/Source/Saml2.Authentication.Core/Bindings/HttpRedirectBinding.cs b/Source/Saml2.Authentication.Core/Bindings/HttpRedirectBinding.cs index 6743487..57436d3 100644 --- a/Source/Saml2.Authentication.Core/Bindings/HttpRedirectBinding.cs +++ b/Source/Saml2.Authentication.Core/Bindings/HttpRedirectBinding.cs @@ -69,8 +69,7 @@ public bool IsValid() return false; } - var form = Request.Form; - return form != null && form.ContainsKey(SamlResponseQueryKey); + return Request.Form.ContainsKey(SamlResponseQueryKey); } public bool IsLogoutRequest() @@ -85,8 +84,7 @@ public bool IsLogoutRequest() return false; } - var form = Request.Form; - return form != null && form.ContainsKey(SamlRequestQueryKey); + return Request.Form.ContainsKey(SamlRequestQueryKey); } public Saml2Response GetResponse() @@ -106,11 +104,6 @@ public Saml2Response GetResponse() } var form = Request.Form; - if (form == null) - { - return null; - } - return new Saml2Response { Response = form[SamlResponseQueryKey], @@ -130,9 +123,7 @@ public string GetCompressedRelayState() return null; } - var form = Request.Form; - - return form?[SamlRelayStateQueryKey].ToString(); + return Request.Form?[SamlRelayStateQueryKey].ToString(); } public string BuildAuthnRequestUrl(string providerName, Saml2AuthnRequest saml2AuthnRequest, string relayState) diff --git a/Source/Saml2.Authentication.Core/Configuration/Saml2PostConfigureOptions.cs b/Source/Saml2.Authentication.Core/Configuration/Saml2PostConfigureOptions.cs index 464baff..0a0ee54 100644 --- a/Source/Saml2.Authentication.Core/Configuration/Saml2PostConfigureOptions.cs +++ b/Source/Saml2.Authentication.Core/Configuration/Saml2PostConfigureOptions.cs @@ -18,7 +18,7 @@ public Saml2PostConfigureOptions(IDataProtectionProvider dataProtectionProvider) public void PostConfigure(string name, Saml2Options options) { - options.DataProtectionProvider = options.DataProtectionProvider ?? _dataProtectionProvider; + options.DataProtectionProvider ??= _dataProtectionProvider; if (string.IsNullOrEmpty(options.SignOutScheme)) { diff --git a/Source/Saml2.Authentication.Core/Saml2.Authentication.Core.csproj b/Source/Saml2.Authentication.Core/Saml2.Authentication.Core.csproj index f23922a..1c2d453 100644 --- a/Source/Saml2.Authentication.Core/Saml2.Authentication.Core.csproj +++ b/Source/Saml2.Authentication.Core/Saml2.Authentication.Core.csproj @@ -1,12 +1,12 @@  - netcoreapp3.1 + net5.0 true true true snupkg Saml2.Authentication.Core - A SAML 2.0 authentication middleware for ASP.NET Core + SAML 2.0 authentication middleware for ASP.NET Core Jessy Kyalo Musyimi Saml2.Authentication.Core @@ -18,7 +18,7 @@ - + From 0a7c6d6429cc1a862383ab3d0e35579fbb922e0a Mon Sep 17 00:00:00 2001 From: Jessy Kyalo Musyimi Date: Fri, 21 May 2021 10:16:23 +0200 Subject: [PATCH 2/2] [patch] Show logged in user info --- Source/DemoWebApp/Views/Home/About.cshtml | 7 -- Source/DemoWebApp/Views/Home/Contact.cshtml | 17 --- Source/DemoWebApp/Views/Home/Index.cshtml | 118 +++--------------- .../Views/Manage/ChangePassword.cshtml | 2 +- .../Views/Manage/ExternalLogins.cshtml | 2 +- Source/DemoWebApp/Views/Manage/Index.cshtml | 2 +- .../Views/Manage/SetPassword.cshtml | 2 +- Source/DemoWebApp/Views/Shared/_Layout.cshtml | 5 - 8 files changed, 22 insertions(+), 133 deletions(-) delete mode 100644 Source/DemoWebApp/Views/Home/About.cshtml delete mode 100644 Source/DemoWebApp/Views/Home/Contact.cshtml diff --git a/Source/DemoWebApp/Views/Home/About.cshtml b/Source/DemoWebApp/Views/Home/About.cshtml deleted file mode 100644 index 3674e37..0000000 --- a/Source/DemoWebApp/Views/Home/About.cshtml +++ /dev/null @@ -1,7 +0,0 @@ -@{ - ViewData["Title"] = "About"; -} -

@ViewData["Title"]

-

@ViewData["Message"]

- -

Use this area to provide additional information.

diff --git a/Source/DemoWebApp/Views/Home/Contact.cshtml b/Source/DemoWebApp/Views/Home/Contact.cshtml deleted file mode 100644 index a11a186..0000000 --- a/Source/DemoWebApp/Views/Home/Contact.cshtml +++ /dev/null @@ -1,17 +0,0 @@ -@{ - ViewData["Title"] = "Contact"; -} -

@ViewData["Title"]

-

@ViewData["Message"]

- -
- One Microsoft Way
- Redmond, WA 98052-6399
- P: - 425.555.0100 -
- -
- Support: Support@example.com
- Marketing: Marketing@example.com -
diff --git a/Source/DemoWebApp/Views/Home/Index.cshtml b/Source/DemoWebApp/Views/Home/Index.cshtml index f804781..d909375 100644 --- a/Source/DemoWebApp/Views/Home/Index.cshtml +++ b/Source/DemoWebApp/Views/Home/Index.cshtml @@ -1,106 +1,24 @@ -@{ +@using System.Security.Claims +@{ ViewData["Title"] = "Home Page"; } - -
-
-

Application uses

-
    -
  • Sample pages using ASP.NET Core MVC
  • -
  • Theming using Bootstrap
  • -
-
- - -
-

Run & Deploy

+
+ @if (Context.User.Identities.Any(i => i.IsAuthenticated)) + { +

User is logged in as @Context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value

+

Claims:

-
-
+ } + else + { +

User is not logged in

+ } + + \ No newline at end of file diff --git a/Source/DemoWebApp/Views/Manage/ChangePassword.cshtml b/Source/DemoWebApp/Views/Manage/ChangePassword.cshtml index 0f7b041..5e93293 100644 --- a/Source/DemoWebApp/Views/Manage/ChangePassword.cshtml +++ b/Source/DemoWebApp/Views/Manage/ChangePassword.cshtml @@ -5,7 +5,7 @@ }

@ViewData["Title"]

-@Html.Partial("_StatusMessage", Model.StatusMessage) +@Html.PartialAsync("_StatusMessage", Model.StatusMessage)
diff --git a/Source/DemoWebApp/Views/Manage/ExternalLogins.cshtml b/Source/DemoWebApp/Views/Manage/ExternalLogins.cshtml index e6f5687..ec064dd 100644 --- a/Source/DemoWebApp/Views/Manage/ExternalLogins.cshtml +++ b/Source/DemoWebApp/Views/Manage/ExternalLogins.cshtml @@ -4,7 +4,7 @@ ViewData.AddActivePage(ManageNavPages.ExternalLogins); } -@Html.Partial("_StatusMessage", Model.StatusMessage) +@Html.PartialAsync("_StatusMessage", Model.StatusMessage) @if (Model.CurrentLogins?.Count > 0) {

Registered Logins

diff --git a/Source/DemoWebApp/Views/Manage/Index.cshtml b/Source/DemoWebApp/Views/Manage/Index.cshtml index c141c95..63b0c4f 100644 --- a/Source/DemoWebApp/Views/Manage/Index.cshtml +++ b/Source/DemoWebApp/Views/Manage/Index.cshtml @@ -5,7 +5,7 @@ }

@ViewData["Title"]

-@Html.Partial("_StatusMessage", Model.StatusMessage) +@Html.PartialAsync("_StatusMessage", Model.StatusMessage)
diff --git a/Source/DemoWebApp/Views/Manage/SetPassword.cshtml b/Source/DemoWebApp/Views/Manage/SetPassword.cshtml index 56c3599..65da228 100644 --- a/Source/DemoWebApp/Views/Manage/SetPassword.cshtml +++ b/Source/DemoWebApp/Views/Manage/SetPassword.cshtml @@ -5,7 +5,7 @@ }

Set your password

-@Html.Partial("_StatusMessage", Model.StatusMessage) +@Html.PartialAsync("_StatusMessage", Model.StatusMessage)

You do not have a local username/password for this site. Add a local account so you can log in without an external login. diff --git a/Source/DemoWebApp/Views/Shared/_Layout.cshtml b/Source/DemoWebApp/Views/Shared/_Layout.cshtml index fe71eff..dfcc6ac 100644 --- a/Source/DemoWebApp/Views/Shared/_Layout.cshtml +++ b/Source/DemoWebApp/Views/Shared/_Layout.cshtml @@ -29,11 +29,6 @@ DemoWebApp