Skip to content

Commit 63f66c5

Browse files
committed
AOT support added
1 parent 5972e5f commit 63f66c5

10 files changed

+284
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public void ConfigureServices(IServiceCollection services)
300300
## Release Notes
301301
| Version |           Notes |
302302
|---------|-------|
303-
|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> |
303+
|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> |
304304
|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> |
305305
|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> |
306306
|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> |

samples/SampleWebApi_AOT/Program.cs

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
using AspNetCore.Authentication.Basic;
2+
using Microsoft.AspNetCore.Authentication;
3+
using Microsoft.AspNetCore.Authorization;
4+
using SampleWebApi.Repositories;
5+
using SampleWebApi.Services;
6+
using System.Text;
7+
using System.Text.Json.Serialization;
8+
9+
var builder = WebApplication.CreateSlimBuilder(args);
10+
builder.WebHost.UseKestrelHttpsConfiguration();
11+
12+
// Add User repository to the dependency container.
13+
builder.Services.AddTransient<IUserRepository, InMemoryUserRepository>();
14+
15+
// Add the Basic scheme authentication here..
16+
// It requires Realm to be set in the options if SuppressWWWAuthenticateHeader is not set.
17+
// 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.
18+
builder.Services.AddAuthentication(BasicDefaults.AuthenticationScheme)
19+
20+
// 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.
21+
// Please note if both the delgate and validation server are set then the delegate will be used instead of BasicUserValidationService.
22+
//.AddBasic(options =>
23+
24+
// The below AddBasic with type parameter will add the BasicUserValidationService to the dependency register.
25+
// Please note if OnValidateCredentials delegete on options.Events is also set then this delegate will be used instead of BasicUserValidationService.
26+
.AddBasic<BasicUserValidationService>(options =>
27+
{
28+
options.Realm = "Sample Web API";
29+
30+
//// Optional option to suppress the browser login dialog for ajax calls.
31+
//options.SuppressWWWAuthenticateHeader = true;
32+
33+
//// Optional option to ignore authentication if AllowAnonumous metadata/filter attribute is added to an endpoint.
34+
//options.IgnoreAuthenticationIfAllowAnonymous = true;
35+
36+
//// Optional events to override the basic original logic with custom logic.
37+
//// Only use this if you know what you are doing at your own risk. Any of the events can be assigned.
38+
options.Events = new BasicEvents
39+
{
40+
41+
//// A delegate assigned to this property will be invoked just before validating credentials.
42+
//OnValidateCredentials = async (context) =>
43+
//{
44+
// // custom code to handle credentials, create principal and call Success method on context.
45+
// var userRepository = context.HttpContext.RequestServices.GetRequiredService<IUserRepository>();
46+
// var user = await userRepository.GetUserByUsername(context.Username);
47+
// var isValid = user != null && user.Password == context.Password;
48+
// if (isValid)
49+
// {
50+
// context.Response.Headers.Add("ValidationCustomHeader", "From OnValidateCredentials");
51+
// var claims = new[]
52+
// {
53+
// new Claim(ClaimTypes.NameIdentifier, context.Username, ClaimValueTypes.String, context.Options.ClaimsIssuer),
54+
// new Claim(ClaimTypes.Name, context.Username, ClaimValueTypes.String, context.Options.ClaimsIssuer),
55+
// new Claim("CustomClaimType", "Custom Claim Value - from OnValidateCredentials")
56+
// };
57+
// context.Principal = new ClaimsPrincipal(new ClaimsIdentity(claims, context.Scheme.Name));
58+
// context.Success();
59+
// }
60+
// else
61+
// {
62+
// context.NoResult();
63+
// }
64+
//},
65+
66+
//// A delegate assigned to this property will be invoked just before validating credentials.
67+
//// NOTE: Same as above delegate but slightly different implementation which will give same result.
68+
//OnValidateCredentials = async (context) =>
69+
//{
70+
// // custom code to handle credentials, create principal and call Success method on context.
71+
// var userRepository = context.HttpContext.RequestServices.GetRequiredService<IUserRepository>();
72+
// var user = await userRepository.GetUserByUsername(context.Username);
73+
// var isValid = user != null && user.Password == context.Password;
74+
// if (isValid)
75+
// {
76+
// context.Response.Headers.Add("ValidationCustomHeader", "From OnValidateCredentials");
77+
// var claims = new[]
78+
// {
79+
// new Claim("CustomClaimType", "Custom Claim Value - from OnValidateCredentials")
80+
// };
81+
// context.ValidationSucceeded(claims); // claims are optional
82+
// }
83+
// else
84+
// {
85+
// context.ValidationFailed();
86+
// }
87+
//},
88+
89+
//// A delegate assigned to this property will be invoked before a challenge is sent back to the caller when handling unauthorized response.
90+
//OnHandleChallenge = async (context) =>
91+
//{
92+
// // custom code to handle authentication challenge unauthorized response.
93+
// context.Response.StatusCode = StatusCodes.Status401Unauthorized;
94+
// context.Response.Headers.Add("ChallengeCustomHeader", "From OnHandleChallenge");
95+
// await context.Response.WriteAsync("{\"CustomBody\":\"From OnHandleChallenge\"}");
96+
// context.Handled(); // important! do not forget to call this method at the end.
97+
//},
98+
99+
//// A delegate assigned to this property will be invoked if Authorization fails and results in a Forbidden response.
100+
//OnHandleForbidden = async (context) =>
101+
//{
102+
// // custom code to handle forbidden response.
103+
// context.Response.StatusCode = StatusCodes.Status403Forbidden;
104+
// context.Response.Headers.Add("ForbidCustomHeader", "From OnHandleForbidden");
105+
// await context.Response.WriteAsync("{\"CustomBody\":\"From OnHandleForbidden\"}");
106+
// context.Handled(); // important! do not forget to call this method at the end.
107+
//},
108+
109+
//// A delegate assigned to this property will be invoked when the authentication succeeds. It will not be called if OnValidateCredentials delegate is assigned.
110+
//// It can be used for adding claims, headers, etc to the response.
111+
//OnAuthenticationSucceeded = (context) =>
112+
//{
113+
// //custom code to add extra bits to the success response.
114+
// context.Response.Headers.Add("SuccessCustomHeader", "From OnAuthenticationSucceeded");
115+
// var customClaims = new List<Claim>
116+
// {
117+
// new Claim("CustomClaimType", "Custom Claim Value - from OnAuthenticationSucceeded")
118+
// };
119+
// context.AddClaims(customClaims);
120+
// //or can add like this - context.Principal.AddIdentity(new ClaimsIdentity(customClaims));
121+
// return Task.CompletedTask;
122+
//},
123+
124+
//// A delegate assigned to this property will be invoked when the authentication fails.
125+
//OnAuthenticationFailed = (context) =>
126+
//{
127+
// // custom code to handle failed authentication.
128+
// context.Fail("Failed to authenticate");
129+
// return Task.CompletedTask;
130+
//}
131+
132+
};
133+
});
134+
135+
// All the requests will need to be authorized.
136+
// Alternatively, add [Authorize] attribute to Controller or Action Method where necessary.
137+
builder.Services.AddAuthorizationBuilder()
138+
.SetFallbackPolicy(
139+
new AuthorizationPolicyBuilder()
140+
.RequireAuthenticatedUser()
141+
.Build()
142+
);
143+
144+
145+
builder.Services.ConfigureHttpJsonOptions(options =>
146+
{
147+
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
148+
});
149+
150+
var app = builder.Build();
151+
152+
app.UseHttpsRedirection();
153+
154+
app.UseAuthentication(); // NOTE: DEFAULT TEMPLATE DOES NOT HAVE THIS, THIS LINE IS REQUIRED AND HAS TO BE ADDED!!!
155+
156+
app.UseAuthorization();
157+
158+
var valuesApi = app.MapGroup("/api/values")
159+
.RequireAuthorization();
160+
valuesApi.MapGet("/", () => new[] { "value1", "value2" });
161+
valuesApi.MapGet("/claims", async (context) =>
162+
{
163+
var sb = new StringBuilder();
164+
foreach (var claim in context.User.Claims)
165+
{
166+
sb.AppendLine($"{claim.Type}: {claim.Value}");
167+
}
168+
context.Response.StatusCode = 200;
169+
await context.Response.WriteAsync(sb.ToString());
170+
});
171+
valuesApi.MapGet("/forbid", async (context) =>
172+
{
173+
await context.ForbidAsync();
174+
});
175+
176+
app.Run();
177+
178+
179+
[JsonSerializable(typeof(string))]
180+
[JsonSerializable(typeof(string[]))]
181+
internal partial class AppJsonSerializerContext : JsonSerializerContext
182+
{
183+
184+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"https": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"launchUrl": "api/values",
9+
"applicationUrl": "https://localhost:44304;http://localhost:3920",
10+
"sslPort": 44304,
11+
"useSSL": true,
12+
"environmentVariables": {
13+
"ASPNETCORE_ENVIRONMENT": "Development"
14+
}
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<InvariantGlobalization>true</InvariantGlobalization>
8+
<PublishAot>true</PublishAot>
9+
</PropertyGroup>
10+
11+
<Import Project="..\SampleWebApi.Shared\SampleWebApi.Shared.projitems" Label="Shared" />
12+
13+
<!--<ItemGroup>
14+
<PackageReference Include="AspNetCore.Authentication.Basic" Version="9.0.0" />
15+
</ItemGroup>-->
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\src\AspNetCore.Authentication.Basic\AspNetCore.Authentication.Basic.csproj" />
19+
<TrimmerRootAssembly Include="AspNetCore.Authentication.Basic" />
20+
</ItemGroup>
21+
22+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

src/AspNetCore.Authentication.Basic.sln

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApi_8_0", "..\samp
4040
EndProject
4141
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApi_9_0", "..\samples\SampleWebApi_9_0\SampleWebApi_9_0.csproj", "{53DBA160-9C57-4A01-A189-34FD4D052CD1}"
4242
EndProject
43+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApi_AOT", "..\samples\SampleWebApi_AOT\SampleWebApi_AOT.csproj", "{79C7438E-72FC-4C43-B255-7B5D1AB94761}"
44+
EndProject
4345
Global
4446
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4547
Debug|Any CPU = Debug|Any CPU
@@ -86,6 +88,10 @@ Global
8688
{53DBA160-9C57-4A01-A189-34FD4D052CD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
8789
{53DBA160-9C57-4A01-A189-34FD4D052CD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
8890
{53DBA160-9C57-4A01-A189-34FD4D052CD1}.Release|Any CPU.Build.0 = Release|Any CPU
91+
{79C7438E-72FC-4C43-B255-7B5D1AB94761}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
92+
{79C7438E-72FC-4C43-B255-7B5D1AB94761}.Debug|Any CPU.Build.0 = Debug|Any CPU
93+
{79C7438E-72FC-4C43-B255-7B5D1AB94761}.Release|Any CPU.ActiveCfg = Release|Any CPU
94+
{79C7438E-72FC-4C43-B255-7B5D1AB94761}.Release|Any CPU.Build.0 = Release|Any CPU
8995
EndGlobalSection
9096
GlobalSection(SolutionProperties) = preSolution
9197
HideSolutionNode = FALSE
@@ -101,6 +107,7 @@ Global
101107
{D5C8BCC5-C997-475E-9E71-5BF807294BB6} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
102108
{548DBE4A-0C06-4FB3-BEA6-6AB4A0386EEF} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
103109
{53DBA160-9C57-4A01-A189-34FD4D052CD1} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
110+
{79C7438E-72FC-4C43-B255-7B5D1AB94761} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
104111
EndGlobalSection
105112
GlobalSection(ExtensibilityGlobals) = postSolution
106113
SolutionGuid = {70815049-1680-480A-BF5A-00536D6C9C20}
@@ -110,6 +117,7 @@ Global
110117
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{2705db4c-3bce-4cfc-9a30-b4bfd1f28c56}*SharedItemsImports = 5
111118
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{53dba160-9c57-4a01-a189-34fd4d052cd1}*SharedItemsImports = 5
112119
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{548dbe4a-0c06-4fb3-bea6-6ab4a0386eef}*SharedItemsImports = 5
120+
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{79c7438e-72fc-4c43-b255-7b5d1ab94761}*SharedItemsImports = 5
113121
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{897e5c9c-8c0a-4fb6-960c-4d11aafd4491}*SharedItemsImports = 5
114122
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{9232da41-ca69-4fe3-b0c9-d8d85fec272a}*SharedItemsImports = 5
115123
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{b82830a0-fdfc-469d-b2a8-d657cd216451}*SharedItemsImports = 5

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

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
<PackageReleaseNotes>- net9.0 support added
1010
- Sample project for net9.0 added
1111
- Readme updated
12+
- Nullable reference types enabled
13+
- Language version set to latest
14+
- Implicit usings enabled
15+
- AOT support added
1216
</PackageReleaseNotes>
1317
<Description>Easy to use and very light weight Microsoft style Basic Scheme Authentication implementation for ASP.NET Core.</Description>
1418
<Authors>Mihir Dilip</Authors>
@@ -21,6 +25,7 @@
2125
<LangVersion>latest</LangVersion>
2226
<Nullable>enable</Nullable>
2327
<ImplicitUsings>enable</ImplicitUsings>
28+
<IsAotCompatible>true</IsAotCompatible>
2429
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
2530
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
2631
<PackageReadmeFile>README.md</PackageReadmeFile>

0 commit comments

Comments
 (0)