Skip to content

Commit

Permalink
Merge pull request #6 from mihirdilip/5.1.0
Browse files Browse the repository at this point in the history
5.1.0
  • Loading branch information
mihirdilip authored Mar 3, 2021
2 parents 9a0b17b + 7e0c305 commit cc16a19
Show file tree
Hide file tree
Showing 26 changed files with 2,557 additions and 58 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Mihir Dilip
Copyright (c) 2021 Mihir Dilip

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
167 changes: 130 additions & 37 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion samples/SampleWebApi_2_0/SampleWebApi_2_0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.Authentication.Basic" Version="5.0.0" />
<PackageReference Include="AspNetCore.Authentication.Basic" Version="5.1.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion samples/SampleWebApi_2_2/SampleWebApi_2_2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.Authentication.Basic" Version="5.0.0" />
<PackageReference Include="AspNetCore.Authentication.Basic" Version="5.1.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion samples/SampleWebApi_3_1/SampleWebApi_3_1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Import Project="..\SampleWebApi.Shared\SampleWebApi.Shared.projitems" Label="Shared" />

<ItemGroup>
<PackageReference Include="AspNetCore.Authentication.Basic" Version="5.0.0" />
<PackageReference Include="AspNetCore.Authentication.Basic" Version="5.1.0" />
</ItemGroup>

<!--<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion samples/SampleWebApi_5_0/SampleWebApi_5_0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Import Project="..\SampleWebApi.Shared\SampleWebApi.Shared.projitems" Label="Shared" />

<ItemGroup>
<PackageReference Include="AspNetCore.Authentication.Basic" Version="5.0.0" />
<PackageReference Include="AspNetCore.Authentication.Basic" Version="5.1.0" />
</ItemGroup>

<!--<ItemGroup>
Expand Down
11 changes: 10 additions & 1 deletion src/AspNetCore.Authentication.Basic.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApi_2_0", "..\samp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApi_3_1", "..\samples\SampleWebApi_3_1\SampleWebApi_3_1.csproj", "{2705DB4C-3BCE-4CFC-9A30-B4BFD1F28C56}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApi_5_0", "..\samples\SampleWebApi_5_0\SampleWebApi_5_0.csproj", "{B82830A0-FDFC-469D-B2A8-D657CD216451}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApi_5_0", "..\samples\SampleWebApi_5_0\SampleWebApi_5_0.csproj", "{B82830A0-FDFC-469D-B2A8-D657CD216451}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F7494366-ED1D-4342-AE5D-DD6BE67C63DF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore.Authentication.Basic.Tests", "..\test\AspNetCore.Authentication.Basic.Tests\AspNetCore.Authentication.Basic.Tests.csproj", "{335B0D1F-A428-4D2E-AF87-269C75F5F138}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
Expand Down Expand Up @@ -57,6 +61,10 @@ Global
{B82830A0-FDFC-469D-B2A8-D657CD216451}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B82830A0-FDFC-469D-B2A8-D657CD216451}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B82830A0-FDFC-469D-B2A8-D657CD216451}.Release|Any CPU.Build.0 = Release|Any CPU
{335B0D1F-A428-4D2E-AF87-269C75F5F138}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{335B0D1F-A428-4D2E-AF87-269C75F5F138}.Debug|Any CPU.Build.0 = Debug|Any CPU
{335B0D1F-A428-4D2E-AF87-269C75F5F138}.Release|Any CPU.ActiveCfg = Release|Any CPU
{335B0D1F-A428-4D2E-AF87-269C75F5F138}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -67,6 +75,7 @@ Global
{897E5C9C-8C0A-4FB6-960C-4D11AAFD4491} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
{2705DB4C-3BCE-4CFC-9A30-B4BFD1F28C56} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
{B82830A0-FDFC-469D-B2A8-D657CD216451} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
{335B0D1F-A428-4D2E-AF87-269C75F5F138} = {F7494366-ED1D-4342-AE5D-DD6BE67C63DF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {70815049-1680-480A-BF5A-00536D6C9C20}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.1;netcoreapp3.0;netstandard2.0;net461</TargetFrameworks>
<Version>5.0.0</Version>
<Version>5.1.0</Version>
<RepositoryUrl>https://github.com/mihirdilip/aspnetcore-authentication-basic/tree/$(Version)</RepositoryUrl>
<PackageProjectUrl>https://github.com/mihirdilip/aspnetcore-authentication-basic/tree/$(Version)</PackageProjectUrl>
<PackageTags>aspnetcore, security, authentication, microsoft, microsoft.aspnetcore.authentication, microsoft-aspnetcore-authentication, microsoft.aspnetcore.authentication.basic, microsoft-aspnetcore-authentication-basic, asp-net-core, netstandard, netstandard20, basic-authentication, basicauthentication, dotnetcore, dotnetcore3.1, net5, net5.0, asp-net-core-basic-authentication, aspnetcore-basic-authentication, net5-basic-authentication, asp-net-core-authentication, aspnetcore-authentication, net5-authentication, asp, aspnet, basic, authentication-scheme</PackageTags>
<PackageReleaseNotes>- .Net 5.0 target framework added
- IgnoreAuthenticationIfAllowAnonymous added to the BasicOptions from netcoreapp3.0 onwards
<PackageReleaseNotes>- Visibility of all the handlers changed to public
</PackageReleaseNotes>
<Description>Easy to use and very light weight Microsoft style Basic Scheme Authentication implementation for ASP.NET Core.</Description>
<Authors>Mihir Dilip</Authors>
<Company>Mihir Dilip</Company>
<Copyright>Copyright (c) 2020 Mihir Dilip</Copyright>
<Copyright>Copyright (c) 2021 Mihir Dilip</Copyright>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Title>$(AssemblyName)</Title>
<RepositoryType>git</RepositoryType>
Expand Down Expand Up @@ -44,6 +43,14 @@
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<!-- Unit Testing - To make internal visible to tests -->
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>
$(MSBuildProjectName).Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100196de3cf1767e4651bd29928a1dfdc149897770adb2439fd0f42a480f0f0c955606d6e6cae87cbdcdfbd1686ec7775789b82fa869387c31dcc740991f3d3d2db407b0a28d772fe343cdf8f1b12ee53bb625aee854f56e0be93f11c307c0dbc54aad277111fc04db0eed2513a20d5663c50412afd34e8c8fb98459d8f4d90e5b6</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\Release\$(TargetFramework)\AspNetCore.Authentication.Basic.xml</DocumentationFile>
</PropertyGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/AspNetCore.Authentication.Basic/BasicHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace AspNetCore.Authentication.Basic
/// <summary>
/// Inherited from <see cref="AuthenticationHandler{TOptions}"/> for basic authentication.
/// </summary>
internal class BasicHandler : AuthenticationHandler<BasicOptions>
public class BasicHandler : AuthenticationHandler<BasicOptions>
{
/// <summary>
/// Basic Handler Constructor.
Expand Down Expand Up @@ -253,9 +253,9 @@ private BasicCredentials DecodeBasicCredentials(string credentials)
username = usernameAndPasswordSplit[0];
password = usernameAndPasswordSplit[1];
}
catch (Exception)
catch (Exception e)
{
throw new Exception($"Problem decoding '{BasicDefaults.AuthenticationScheme}' scheme credentials.");
throw new Exception($"Problem decoding '{BasicDefaults.AuthenticationScheme}' scheme credentials.", e);
}

if (string.IsNullOrWhiteSpace(username))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void PostConfigure(string name, BasicOptions options)
{
if (!options.SuppressWWWAuthenticateHeader && string.IsNullOrWhiteSpace(options.Realm))
{
throw new InvalidOperationException("Realm must be set in basic options");
throw new InvalidOperationException($"{nameof(BasicOptions.Realm)} must be set in {typeof(BasicOptions).Name} when setting up the authentication.");
}

if (options.Events?.OnValidateCredentials == null && options.EventsType == null && options.BasicUserValidationServiceType == null)
Expand Down
19 changes: 12 additions & 7 deletions src/AspNetCore.Authentication.Basic/BasicUtils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Mihir Dilip. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
Expand All @@ -24,18 +25,22 @@ internal static class BasicUtils
/// <returns></returns>
internal static ClaimsPrincipal BuildClaimsPrincipal(string username, string schemeName, string claimsIssuer, IEnumerable<Claim> claims = null)
{
var claimsList = new List<Claim>();
if (string.IsNullOrWhiteSpace(schemeName)) throw new ArgumentNullException(nameof(schemeName));

var claimsList = new List<Claim>();
if (claims != null) claimsList.AddRange(claims);

if (!claimsList.Any(c => c.Type == ClaimTypes.NameIdentifier))
if (!string.IsNullOrWhiteSpace(username))
{
claimsList.Add(new Claim(ClaimTypes.NameIdentifier, username, ClaimValueTypes.String, claimsIssuer));
}
if (!claimsList.Any(c => c.Type == ClaimTypes.NameIdentifier))
{
claimsList.Add(new Claim(ClaimTypes.NameIdentifier, username, ClaimValueTypes.String, claimsIssuer));
}

if (!claimsList.Any(c => c.Type == ClaimTypes.Name))
{
claimsList.Add(new Claim(ClaimTypes.Name, username, ClaimValueTypes.String, claimsIssuer));
if (!claimsList.Any(c => c.Type == ClaimTypes.Name))
{
claimsList.Add(new Claim(ClaimTypes.Name, username, ClaimValueTypes.String, claimsIssuer));
}
}

return new ClaimsPrincipal(new ClaimsIdentity(claimsList, schemeName));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.1;netcoreapp3.0;netcoreapp2.1;net461</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<PropertyGroup>
<!-- Strong Name Key -->
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>$(SolutionDir)key.snk</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="5.0.3" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.11" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.0.3" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="5.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="5.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AspNetCore.Authentication.Basic\AspNetCore.Authentication.Basic.csproj" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions test/AspNetCore.Authentication.Basic.Tests/BasicDefaultsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Mihir Dilip. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Xunit;

namespace AspNetCore.Authentication.Basic.Tests
{
public class BasicDefaultsTests
{
[Fact]
public void AuthenticationSchemeValueTest()
{
Assert.Equal("Basic", BasicDefaults.AuthenticationScheme);
}
}
}
Loading

0 comments on commit cc16a19

Please sign in to comment.