Skip to content

Commit ca7b2e2

Browse files
committed
Added direct references to latest framework and removed no longer supported frameworks.
Added possibility to suppress WWWAuthenticate header globally not only on Ajax request.
1 parent 05ac92c commit ca7b2e2

File tree

12 files changed

+90
-37
lines changed

12 files changed

+90
-37
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017 Marcin Smółka
3+
Copyright (c) 2017 - 2020 Marcin Smółka
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Install using the [ZNetCS.AspNetCore.Authentication.Basic NuGet package](https:/
1111
```
1212
PM> Install-Package ZNetCS.AspNetCore.Authentication.Basic
1313
```
14+
# Changes in 5.0.0
15+
Added direct references to latest framework and removed no longer supported frameworks.
16+
Added possibility to suppress WWWAuthenticate header globally not only on Ajax request.
1417

1518
# Important change in 4.0.0
1619
From now assembly is signed.
@@ -26,7 +29,7 @@ When you install the package, it should be added to your `.csproj`. Alternativel
2629

2730
```xml
2831
<ItemGroup>
29-
<PackageReference Include="ZNetCS.AspNetCore.Authentication.Basic" Version="4.0.0" />
32+
<PackageReference Include="ZNetCS.AspNetCore.Authentication.Basic" Version="5.0.0" />
3033
</ItemGroup>
3134
```
3235

ZNetCS.AspNetCore.Authentication.Basic.sln

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26228.4
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30717.126
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EC6AC5C8-ACCF-492E-99E6-28E935E993C4}"
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{EBBE213E-359F-4158-9AB8-3C46BDBAA30C}"
99
ProjectSection(SolutionItems) = preProject
10+
LICENSE = LICENSE
1011
README.md = README.md
1112
EndProjectSection
1213
EndProject
@@ -38,4 +39,7 @@ Global
3839
{0C36B232-F48E-4BCC-8F11-840A9694B75D} = {EC6AC5C8-ACCF-492E-99E6-28E935E993C4}
3940
{16932A67-81EA-4D76-873C-C22FA81F1A2D} = {3D8699E9-972B-47E0-9A9F-FEA369A9C832}
4041
EndGlobalSection
42+
GlobalSection(ExtensibilityGlobals) = postSolution
43+
SolutionGuid = {C9D04CE8-4159-4CA2-AE7F-8BEF87D7ACC3}
44+
EndGlobalSection
4145
EndGlobal

src/ZNetCS.AspNetCore.Authentication.Basic/AjaxRequestOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class AjaxRequestOptions
3030
/// Gets or sets a value indicating whether suppress sending the WWWAuthenticate response header when a request has the
3131
/// header (X-Requested-With,XMLHttpRequest).
3232
/// </summary>
33-
public bool SuppressWwwAuthenticateHeader { get; set; } = false;
33+
public bool SuppressWwwAuthenticateHeader { get; set; }
3434

3535
#endregion
3636
}

src/ZNetCS.AspNetCore.Authentication.Basic/BasicAuthenticationHandler.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace ZNetCS.AspNetCore.Authentication.Basic
1212
#region Usings
1313

1414
using System;
15+
using System.Diagnostics.CodeAnalysis;
1516
using System.Linq;
1617
using System.Text;
1718
using System.Text.Encodings.Web;
@@ -100,7 +101,7 @@ public BasicAuthenticationHandler(IOptionsMonitor<BasicAuthenticationOptions> op
100101
protected override Task<object> CreateEventsAsync() => Task.FromResult<object>(new BasicAuthenticationEvents());
101102

102103
/// <inheritdoc/>
103-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Just for validation, the quickest")]
104+
[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Just for validation, the quickest")]
104105
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
105106
{
106107
// RFC 7230 section 3.2.2
@@ -171,6 +172,11 @@ protected override Task HandleChallengeAsync(AuthenticationProperties context)
171172
{
172173
this.Response.StatusCode = StatusCodes.Status401Unauthorized;
173174

175+
if (this.Options.SuppressWwwAuthenticateHeader)
176+
{
177+
return Task.CompletedTask;
178+
}
179+
174180
if ((this.Options.AjaxRequestOptions?.SuppressWwwAuthenticateHeader == true)
175181
&& this.Request.Headers.TryGetValue(
176182
this.Options.AjaxRequestOptions?.HeaderName ?? BasicAuthenticationDefaults.AjaxRequestHeaderName,

src/ZNetCS.AspNetCore.Authentication.Basic/BasicAuthenticationOptions.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ namespace ZNetCS.AspNetCore.Authentication.Basic
1111
{
1212
#region Usings
1313

14-
using System.Diagnostics.CodeAnalysis;
15-
1614
using Microsoft.AspNetCore.Authentication;
1715

1816
using ZNetCS.AspNetCore.Authentication.Basic.Events;
@@ -41,7 +39,7 @@ public BasicAuthenticationOptions()
4139
#region Public Properties
4240

4341
/// <summary>
44-
/// Gets or sets the ajax request options.
42+
/// Gets or sets the ajax request options. Special authentication header handling for Ajax requests.
4543
/// </summary>
4644
public AjaxRequestOptions AjaxRequestOptions { get; set; }
4745

@@ -75,9 +73,14 @@ public BasicAuthenticationOptions()
7573
/// authentication scheme.Note that a response can have multiple
7674
/// challenges with the same auth-scheme but with different realms.
7775
/// </remarks>
78-
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "OK")]
7976
public string Realm { get; set; }
8077

78+
/// <summary>
79+
/// Gets or sets a value indicating whether suppress sending the WWWAuthenticate response header.
80+
/// In some rest scenarios it is not needed, so can be suppressed.
81+
/// </summary>
82+
public bool SuppressWwwAuthenticateHeader { get; set; }
83+
8184
#endregion
8285
}
8386
}

src/ZNetCS.AspNetCore.Authentication.Basic/ZNetCS.AspNetCore.Authentication.Basic.csproj

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,40 @@
44
<Description>A simple basic authentication middleware.</Description>
55
<Copyright>Marcin Smółka zNET Computer Solutions</Copyright>
66
<Authors>Marcin Smółka</Authors>
7-
<TargetFrameworks>net461;netstandard2.0;netcoreapp3.0</TargetFrameworks>
8-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
7+
<TargetFrameworks>net461;netstandard2.0;netcoreapp3.1;net5.0</TargetFrameworks>
98
<AssemblyName>ZNetCS.AspNetCore.Authentication.Basic</AssemblyName>
10-
<PackageId>ZNetCS.AspNetCore.Authentication.Basic</PackageId>
11-
<PackageTags>aspnetcore;aspnetcoremvc;middleware;authentication;basic;ASP.NET;MVC</PackageTags>
12-
<PackageProjectUrl>https://github.com/msmolka/ZNetCS.AspNetCore.Authentication.Basic</PackageProjectUrl>
13-
<PackageLicenseUrl>https://raw.githubusercontent.com/msmolka/ZNetCS.AspNetCore.Authentication.Basic/master/LICENSE</PackageLicenseUrl>
14-
<PackageReleaseNotes>Library is now signed.</PackageReleaseNotes>
15-
<RepositoryType>git</RepositoryType>
16-
<RepositoryUrl>https://github.com/msmolka/ZNetCS.AspNetCore.Authentication.Basic</RepositoryUrl>
17-
<VersionPrefix>4.0.0</VersionPrefix>
18-
<NoWarn>$(NoWarn);NU5125</NoWarn>
19-
<!-- remove once tools are truly ready for NuGet's new 'license' element -->
9+
<VersionPrefix>5.0.0</VersionPrefix>
2010
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
2111
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
2212
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
2313
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
2414
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
2515
<CodeAnalysisRuleSet>..\..\CommonRuleSet.ruleset</CodeAnalysisRuleSet>
26-
<LangVersion>8.0</LangVersion>
2716
<SignAssembly>true</SignAssembly>
2817
<AssemblyOriginatorKeyFile>StrongNameKey.snk</AssemblyOriginatorKeyFile>
18+
<LangVersion>Latest</LangVersion>
2919
</PropertyGroup>
3020

21+
<PropertyGroup>
22+
<PackageId>ZNetCS.AspNetCore.Authentication.Basic</PackageId>
23+
<PackageTags>aspnetcore;aspnetcoremvc;middleware;authentication;basic;</PackageTags>
24+
<PackageProjectUrl>https://github.com/msmolka/ZNetCS.AspNetCore.Authentication.Basic</PackageProjectUrl>
25+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
26+
<PackageReleaseNotes>Update support for latest supported frameworks. Allow suppress WWWAuthenticate globally.</PackageReleaseNotes>
27+
</PropertyGroup>
28+
29+
<PropertyGroup>
30+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
31+
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
32+
<EmbedUntrackedSources>true</EmbedUntrackedSources>
33+
<RepositoryType>git</RepositoryType>
34+
<RepositoryUrl>https://github.com/msmolka/ZNetCS.AspNetCore.Authentication.Basic</RepositoryUrl>
35+
</PropertyGroup>
36+
37+
3138
<ItemGroup>
3239
<AdditionalFiles Include="..\..\stylecop.json" Link="stylecop.json" />
40+
<None Include="..\..\LICENSE" Pack="true" PackagePath=""/>
3341
</ItemGroup>
3442

3543
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
@@ -38,7 +46,7 @@
3846

3947
<ItemGroup>
4048
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
41-
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
49+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.1">
4250
<PrivateAssets>all</PrivateAssets>
4351
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4452
</PackageReference>
@@ -48,7 +56,7 @@
4856
</PackageReference>
4957
</ItemGroup>
5058

51-
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
59+
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1' OR '$(TargetFramework)' == 'net5.0'">
5260
<FrameworkReference Include="Microsoft.AspNetCore.App" />
5361
</ItemGroup>
5462

test/ZNetCS.AspNetCore.Authentication.BasicTests/AuthorizationTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,30 @@ public async Task UnauthorizedMyRealmTestAjaxRequestSuppressed()
268268
Assert.IsNull(wwwAuth, "No header should be sent back on ajax request");
269269
}
270270

271+
/// <summary>
272+
/// The unauthorized basic realm via ajax.
273+
/// </summary>
274+
[TestMethod]
275+
public async Task UnauthorizedMyRealmTestRequestSuppressed()
276+
{
277+
using var server = new TestServer(WebHostBuilderHelper.CreateBuilder(o =>
278+
{
279+
o.Realm = "My realm";
280+
o.SuppressWwwAuthenticateHeader = true;
281+
}));
282+
283+
using HttpClient client = server.CreateClient();
284+
285+
// Act
286+
HttpResponseMessage response = await client.GetAsync("api/test");
287+
288+
// Assert
289+
AuthenticationHeaderValue wwwAuth = response.Headers.WwwAuthenticate.SingleOrDefault();
290+
291+
Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode, "StatusCode != Unauthorized");
292+
Assert.IsNull(wwwAuth, "No header should be sent back on ajax request");
293+
}
294+
271295
#endregion
272296
}
273297
}

test/ZNetCS.AspNetCore.Authentication.BasicTests/Startup.cs renamed to test/ZNetCS.AspNetCore.Authentication.BasicTests/Startup1.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// --------------------------------------------------------------------------------------------------------------------
2-
// <copyright file="Startup.cs" company="Marcin Smółka zNET Computer Solutions">
2+
// <copyright file="Startup1.cs" company="Marcin Smółka zNET Computer Solutions">
33
// Copyright (c) Marcin Smółka zNET Computer Solutions. All rights reserved.
44
// </copyright>
55
// <summary>
66
// The startup.
77
// </summary>
88
// --------------------------------------------------------------------------------------------------------------------
99

10-
#if !NETCOREAPP3_0
10+
#if !NETCOREAPP3_1 && !NET5_0
1111
namespace ZNetCS.AspNetCore.Authentication.BasicTests
1212
{
1313
#region Usings

test/ZNetCS.AspNetCore.Authentication.BasicTests/Startup3.cs renamed to test/ZNetCS.AspNetCore.Authentication.BasicTests/Startup2.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// --------------------------------------------------------------------------------------------------------------------
2-
// <copyright file="Startup3.cs" company="Marcin Smółka zNET Computer Solutions">
2+
// <copyright file="Startup2.cs" company="Marcin Smółka zNET Computer Solutions">
33
// Copyright (c) Marcin Smółka zNET Computer Solutions. All rights reserved.
44
// </copyright>
55
// <summary>
66
// The startup.
77
// </summary>
88
// --------------------------------------------------------------------------------------------------------------------
99

10-
#if NETCOREAPP3_0
10+
#if NETCOREAPP3_1 || NET5_0
1111
namespace ZNetCS.AspNetCore.Authentication.BasicTests
1212
{
1313
#region Usings

0 commit comments

Comments
 (0)