Skip to content

Commit

Permalink
AOT support added
Browse files Browse the repository at this point in the history
  • Loading branch information
mihirdilip committed Jan 9, 2025
1 parent 5972e5f commit 63f66c5
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public void ConfigureServices(IServiceCollection services)
## Release Notes
| Version |           Notes |
|---------|-------|
|9.0.0 | <ul><li>net9.0 support added</li><li>Sample project for net9.0 added</li><li>Readme updated</li><li>Nullable reference types enabled</li><li>Language version set to latest</li><li>Implicit usings enabled</li></ul> |
|9.0.0 | <ul><li>net9.0 support added</li><li>Sample project for net9.0 added</li><li>Readme updated</li><li>Nullable reference types enabled</li><li>Language version set to latest</li><li>Implicit usings enabled</li><li>AOT support added</li></ul> |
|8.0.0 | <ul><li>net8.0 support added</li><li>Sample project for net8.0 added</li><li>BasicSamplesClient.http file added for testing sample projects</li><li>Readme updated</li></ul> |
|7.0.0 | <ul><li>net7.0 support added</li><li>Information log on handler is changed to Debug log when Authorization header is not found on the request</li><li>Added package validations</li><li>Sample project for net7.0 added</li><li>Readme updated</li><li>Readme added to package</li></ul> |
|6.0.1 | <ul><li>net6.0 support added</li><li>Information log on handler is changed to Debug log when IgnoreAuthenticationIfAllowAnonymous is enabled [#9](https://github.com/mihirdilip/aspnetcore-authentication-basic/issues/9)</li><li>Sample project added</li><li>Readme updated</li><li>Copyright year updated on License</li></ul> |
Expand Down
184 changes: 184 additions & 0 deletions samples/SampleWebApi_AOT/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
using AspNetCore.Authentication.Basic;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using SampleWebApi.Repositories;
using SampleWebApi.Services;
using System.Text;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateSlimBuilder(args);
builder.WebHost.UseKestrelHttpsConfiguration();

// Add User repository to the dependency container.
builder.Services.AddTransient<IUserRepository, InMemoryUserRepository>();

// Add the Basic scheme authentication here..
// It requires Realm to be set in the options if SuppressWWWAuthenticateHeader is not set.
// If an implementation of IBasicUserValidationService interface is registered in the dependency register as well as OnValidateCredentials delegete on options.Events is also set then this delegate will be used instead of an implementation of IBasicUserValidationService.
builder.Services.AddAuthentication(BasicDefaults.AuthenticationScheme)

// The below AddBasic without type parameter will require OnValidateCredentials delegete on options.Events to be set unless an implementation of IBasicUserValidationService interface is registered in the dependency register.
// Please note if both the delgate and validation server are set then the delegate will be used instead of BasicUserValidationService.
//.AddBasic(options =>

// The below AddBasic with type parameter will add the BasicUserValidationService to the dependency register.
// Please note if OnValidateCredentials delegete on options.Events is also set then this delegate will be used instead of BasicUserValidationService.
.AddBasic<BasicUserValidationService>(options =>
{
options.Realm = "Sample Web API";

//// Optional option to suppress the browser login dialog for ajax calls.
//options.SuppressWWWAuthenticateHeader = true;

//// Optional option to ignore authentication if AllowAnonumous metadata/filter attribute is added to an endpoint.
//options.IgnoreAuthenticationIfAllowAnonymous = true;

//// Optional events to override the basic original logic with custom logic.
//// Only use this if you know what you are doing at your own risk. Any of the events can be assigned.
options.Events = new BasicEvents
{

//// A delegate assigned to this property will be invoked just before validating credentials.
//OnValidateCredentials = async (context) =>
//{
// // custom code to handle credentials, create principal and call Success method on context.
// var userRepository = context.HttpContext.RequestServices.GetRequiredService<IUserRepository>();
// var user = await userRepository.GetUserByUsername(context.Username);
// var isValid = user != null && user.Password == context.Password;
// if (isValid)
// {
// context.Response.Headers.Add("ValidationCustomHeader", "From OnValidateCredentials");
// var claims = new[]
// {
// new Claim(ClaimTypes.NameIdentifier, context.Username, ClaimValueTypes.String, context.Options.ClaimsIssuer),
// new Claim(ClaimTypes.Name, context.Username, ClaimValueTypes.String, context.Options.ClaimsIssuer),
// new Claim("CustomClaimType", "Custom Claim Value - from OnValidateCredentials")
// };
// context.Principal = new ClaimsPrincipal(new ClaimsIdentity(claims, context.Scheme.Name));
// context.Success();
// }
// else
// {
// context.NoResult();
// }
//},

//// A delegate assigned to this property will be invoked just before validating credentials.
//// NOTE: Same as above delegate but slightly different implementation which will give same result.
//OnValidateCredentials = async (context) =>
//{
// // custom code to handle credentials, create principal and call Success method on context.
// var userRepository = context.HttpContext.RequestServices.GetRequiredService<IUserRepository>();
// var user = await userRepository.GetUserByUsername(context.Username);
// var isValid = user != null && user.Password == context.Password;
// if (isValid)
// {
// context.Response.Headers.Add("ValidationCustomHeader", "From OnValidateCredentials");
// var claims = new[]
// {
// new Claim("CustomClaimType", "Custom Claim Value - from OnValidateCredentials")
// };
// context.ValidationSucceeded(claims); // claims are optional
// }
// else
// {
// context.ValidationFailed();
// }
//},

//// A delegate assigned to this property will be invoked before a challenge is sent back to the caller when handling unauthorized response.
//OnHandleChallenge = async (context) =>
//{
// // custom code to handle authentication challenge unauthorized response.
// context.Response.StatusCode = StatusCodes.Status401Unauthorized;
// context.Response.Headers.Add("ChallengeCustomHeader", "From OnHandleChallenge");
// await context.Response.WriteAsync("{\"CustomBody\":\"From OnHandleChallenge\"}");
// context.Handled(); // important! do not forget to call this method at the end.
//},

//// A delegate assigned to this property will be invoked if Authorization fails and results in a Forbidden response.
//OnHandleForbidden = async (context) =>
//{
// // custom code to handle forbidden response.
// context.Response.StatusCode = StatusCodes.Status403Forbidden;
// context.Response.Headers.Add("ForbidCustomHeader", "From OnHandleForbidden");
// await context.Response.WriteAsync("{\"CustomBody\":\"From OnHandleForbidden\"}");
// context.Handled(); // important! do not forget to call this method at the end.
//},

//// A delegate assigned to this property will be invoked when the authentication succeeds. It will not be called if OnValidateCredentials delegate is assigned.
//// It can be used for adding claims, headers, etc to the response.
//OnAuthenticationSucceeded = (context) =>
//{
// //custom code to add extra bits to the success response.
// context.Response.Headers.Add("SuccessCustomHeader", "From OnAuthenticationSucceeded");
// var customClaims = new List<Claim>
// {
// new Claim("CustomClaimType", "Custom Claim Value - from OnAuthenticationSucceeded")
// };
// context.AddClaims(customClaims);
// //or can add like this - context.Principal.AddIdentity(new ClaimsIdentity(customClaims));
// return Task.CompletedTask;
//},

//// A delegate assigned to this property will be invoked when the authentication fails.
//OnAuthenticationFailed = (context) =>
//{
// // custom code to handle failed authentication.
// context.Fail("Failed to authenticate");
// return Task.CompletedTask;
//}

};
});

// All the requests will need to be authorized.
// Alternatively, add [Authorize] attribute to Controller or Action Method where necessary.
builder.Services.AddAuthorizationBuilder()
.SetFallbackPolicy(
new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build()
);


builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});

var app = builder.Build();

app.UseHttpsRedirection();

app.UseAuthentication(); // NOTE: DEFAULT TEMPLATE DOES NOT HAVE THIS, THIS LINE IS REQUIRED AND HAS TO BE ADDED!!!

app.UseAuthorization();

var valuesApi = app.MapGroup("/api/values")
.RequireAuthorization();
valuesApi.MapGet("/", () => new[] { "value1", "value2" });
valuesApi.MapGet("/claims", async (context) =>
{
var sb = new StringBuilder();
foreach (var claim in context.User.Claims)
{
sb.AppendLine($"{claim.Type}: {claim.Value}");
}
context.Response.StatusCode = 200;
await context.Response.WriteAsync(sb.ToString());
});
valuesApi.MapGet("/forbid", async (context) =>
{
await context.ForbidAsync();
});

app.Run();


[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(string[]))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{

}
17 changes: 17 additions & 0 deletions samples/SampleWebApi_AOT/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl": "https://localhost:44304;http://localhost:3920",
"sslPort": 44304,
"useSSL": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
22 changes: 22 additions & 0 deletions samples/SampleWebApi_AOT/SampleWebApi_AOT.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<PublishAot>true</PublishAot>
</PropertyGroup>

<Import Project="..\SampleWebApi.Shared\SampleWebApi.Shared.projitems" Label="Shared" />

<!--<ItemGroup>
<PackageReference Include="AspNetCore.Authentication.Basic" Version="9.0.0" />
</ItemGroup>-->

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

</Project>
8 changes: 8 additions & 0 deletions samples/SampleWebApi_AOT/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions samples/SampleWebApi_AOT/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
8 changes: 8 additions & 0 deletions src/AspNetCore.Authentication.Basic.sln
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApi_8_0", "..\samp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApi_9_0", "..\samples\SampleWebApi_9_0\SampleWebApi_9_0.csproj", "{53DBA160-9C57-4A01-A189-34FD4D052CD1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApi_AOT", "..\samples\SampleWebApi_AOT\SampleWebApi_AOT.csproj", "{79C7438E-72FC-4C43-B255-7B5D1AB94761}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -86,6 +88,10 @@ Global
{53DBA160-9C57-4A01-A189-34FD4D052CD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{53DBA160-9C57-4A01-A189-34FD4D052CD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{53DBA160-9C57-4A01-A189-34FD4D052CD1}.Release|Any CPU.Build.0 = Release|Any CPU
{79C7438E-72FC-4C43-B255-7B5D1AB94761}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79C7438E-72FC-4C43-B255-7B5D1AB94761}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79C7438E-72FC-4C43-B255-7B5D1AB94761}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79C7438E-72FC-4C43-B255-7B5D1AB94761}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -101,6 +107,7 @@ Global
{D5C8BCC5-C997-475E-9E71-5BF807294BB6} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
{548DBE4A-0C06-4FB3-BEA6-6AB4A0386EEF} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
{53DBA160-9C57-4A01-A189-34FD4D052CD1} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
{79C7438E-72FC-4C43-B255-7B5D1AB94761} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {70815049-1680-480A-BF5A-00536D6C9C20}
Expand All @@ -110,6 +117,7 @@ Global
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{2705db4c-3bce-4cfc-9a30-b4bfd1f28c56}*SharedItemsImports = 5
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{53dba160-9c57-4a01-a189-34fd4d052cd1}*SharedItemsImports = 5
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{548dbe4a-0c06-4fb3-bea6-6ab4a0386eef}*SharedItemsImports = 5
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{79c7438e-72fc-4c43-b255-7b5d1ab94761}*SharedItemsImports = 5
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{897e5c9c-8c0a-4fb6-960c-4d11aafd4491}*SharedItemsImports = 5
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{9232da41-ca69-4fe3-b0c9-d8d85fec272a}*SharedItemsImports = 5
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{b82830a0-fdfc-469d-b2a8-d657cd216451}*SharedItemsImports = 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<PackageReleaseNotes>- net9.0 support added
- Sample project for net9.0 added
- Readme updated
- Nullable reference types enabled
- Language version set to latest
- Implicit usings enabled
- AOT support added
</PackageReleaseNotes>
<Description>Easy to use and very light weight Microsoft style Basic Scheme Authentication implementation for ASP.NET Core.</Description>
<Authors>Mihir Dilip</Authors>
Expand All @@ -21,6 +25,7 @@
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsAotCompatible>true</IsAotCompatible>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
Loading

0 comments on commit 63f66c5

Please sign in to comment.