Skip to content

Commit 4bcb0d8

Browse files
authored
Merge pull request #16 from mihirdilip/5.0.0
5.0.0
2 parents 3b0dac6 + 5517114 commit 4bcb0d8

18 files changed

+396
-28
lines changed

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public class Startup
5454
services.AddControllers();
5555

5656
//// By default, authentication is not challenged for every request which is ASP.NET Core's default intended behaviour.
57-
//// So to challenge authentication for every requests please use below option instead of above services.AddControllers().
58-
//services.AddControllers(options =>
57+
//// So to challenge authentication for every requests please use below FallbackPolicy option.
58+
//services.AddAuthorization(options =>
5959
//{
60-
// options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
60+
// options.FallbackPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
6161
//});
6262
}
6363

@@ -178,16 +178,20 @@ Required to be set. It is the name of the header if it is setup as in-header or
178178
#### Realm
179179
Required to be set if SuppressWWWAuthenticateHeader is not set to true. It is used with WWW-Authenticate response header when challenging un-authenticated requests.
180180

181-
#### ForLegacyIgnoreExtraValidatedApiKeyCheck
182-
Default value is false.
183-
If set to true, IApiKey.Key property returned from IApiKeyProvider.ProvideAsync(string) method is not compared with the key parsed from the request.
184-
This extra check did not existed in the previous version. So you if want to revert back to old version validation, please set this to true.
185-
186181
#### SuppressWWWAuthenticateHeader
187182
Default value is false.
188183
When set to true, it will NOT return WWW-Authenticate response header when challenging un-authenticated requests.
189184
When set to false, it will return WWW-Authenticate response header when challenging un-authenticated requests.
190185

186+
#### IgnoreAuthenticationIfAllowAnonymous
187+
Default value is false.
188+
If set to true, it checks if AllowAnonymous filter on controller action or metadata on the endpoint which, if found, it does not try to authenticate the request.
189+
190+
#### ForLegacyIgnoreExtraValidatedApiKeyCheck
191+
Default value is false.
192+
If set to true, IApiKey.Key property returned from IApiKeyProvider.ProvideAsync(string) method is not compared with the key parsed from the request.
193+
This extra check did not existed in the previous version. So you if want to revert back to old version validation, please set this to true.
194+
191195
#### Events
192196
The object provided by the application to process events raised by the api key authentication middleware.
193197
The application may implement the interface fully, or it may create an instance of ApiKeyEvents and assign delegates only to the events it wants to process.
@@ -222,9 +226,9 @@ Please note that, by default, with ASP.NET Core, all the requests are not challe
222226
However, if you want all the requests to challenge authentication by default, depending on what you are using, you can add the below options line to *ConfigureServices* method on *Startup* class.
223227

224228
```C#
225-
services.AddControllers(options =>
226-
{
227-
options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
229+
services.AddAuthorization(options =>
230+
{
231+
options.FallbackPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
228232
});
229233

230234
// OR

samples/SampleWebApi_2_0/SampleWebApi_2_0.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="3.1.1" />
15+
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="5.0.0" />
1616
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
1717
</ItemGroup>
1818

samples/SampleWebApi_2_2/SampleWebApi_2_2.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="3.1.1" />
9+
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="5.0.0" />
1010
<PackageReference Include="Microsoft.AspNetCore.App" />
1111
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
1212
</ItemGroup>

samples/SampleWebApi_3_1/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"SampleWebApi_3_1": {
2121
"commandName": "Project",
2222
"launchBrowser": true,
23-
"launchUrl": "weatherforecast",
23+
"launchUrl": "api/values",
2424
"environmentVariables": {
2525
"ASPNETCORE_ENVIRONMENT": "Development"
2626
},

samples/SampleWebApi_3_1/SampleWebApi_3_1.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Import Project="..\SampleWebApi.Shared\SampleWebApi.Shared.projitems" Label="Shared" />
88

99
<ItemGroup>
10-
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="3.1.1" />
10+
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="5.0.0" />
1111
</ItemGroup>
1212

1313
<!--<ItemGroup>

samples/SampleWebApi_3_1/Startup.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public void ConfigureServices(IServiceCollection services)
5050
//// Optional option to suppress the browser login dialog for ajax calls.
5151
//options.SuppressWWWAuthenticateHeader = true;
5252

53+
//// Optional option to ignore extra check of ApiKey string after it is validated.
54+
//options.ForLegacyIgnoreExtraValidatedApiKeyCheck = true;
55+
56+
//// Optional option to ignore authentication if AllowAnonumous metadata/filter attribute is added to an endpoint.
57+
//options.IgnoreAuthenticationIfAllowAnonymous = true;
58+
5359
//// Optional events to override the ApiKey original logic with custom logic.
5460
//// Only use this if you know what you are doing at your own risk. Any of the events can be assigned.
5561
options.Events = new ApiKeyEvents
@@ -154,10 +160,16 @@ public void ConfigureServices(IServiceCollection services)
154160
// ALWAYS USE HTTPS (SSL) protocol in production when using ApiKey authentication.
155161
//options.Filters.Add<RequireHttpsAttribute>();
156162

157-
// All the requests will need to be authorized.
158-
// Alternatively, add [Authorize] attribute to Controller or Action Method where necessary.
159-
options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
160163
}); //.AddXmlSerializerFormatters() // To enable XML along with JSON;
164+
165+
// All the requests will need to be authorized.
166+
// Alternatively, add [Authorize] attribute to Controller or Action Method where necessary.
167+
services.AddAuthorization(options =>
168+
{
169+
options.FallbackPolicy = new AuthorizationPolicyBuilder()
170+
.RequireAuthenticatedUser()
171+
.Build();
172+
});
161173
}
162174

163175
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace SampleWebApi_5_0.Controllers
6+
{
7+
[Route("api/[controller]")]
8+
[ApiController]
9+
public class ValuesController : ControllerBase
10+
{
11+
// GET api/values
12+
[HttpGet]
13+
public ActionResult<IEnumerable<string>> Get()
14+
{
15+
return new string[] { "value1", "value2" };
16+
}
17+
18+
[HttpGet("claims")]
19+
public ActionResult<string> Claims()
20+
{
21+
var sb = new StringBuilder();
22+
foreach (var claim in User.Claims)
23+
{
24+
sb.AppendLine($"{claim.Type}: {claim.Value}");
25+
}
26+
return sb.ToString();
27+
}
28+
29+
[HttpGet("forbid")]
30+
public new IActionResult Forbid()
31+
{
32+
return base.Forbid();
33+
}
34+
}
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace SampleWebApi_5_0
5+
{
6+
public class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
CreateHostBuilder(args).Build().Run();
11+
}
12+
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureWebHostDefaults(webBuilder =>
16+
{
17+
webBuilder.UseStartup<Startup>();
18+
});
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:3920",
8+
"sslPort": 44304
9+
}
10+
},
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": true,
15+
"launchUrl": "api/values",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"SampleWebApi_5_0": {
21+
"commandName": "Project",
22+
"dotnetRunMessages": "true",
23+
"launchBrowser": true,
24+
"launchUrl": "api/values",
25+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
}
30+
}
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<Import Project="..\SampleWebApi.Shared\SampleWebApi.Shared.projitems" Label="Shared" />
8+
9+
<ItemGroup>
10+
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="5.0.0" />
11+
</ItemGroup>
12+
13+
<!--<ItemGroup>
14+
<ProjectReference Include="..\..\src\AspNetCore.Authentication.ApiKey\AspNetCore.Authentication.ApiKey.csproj" />
15+
</ItemGroup>-->
16+
17+
</Project>

0 commit comments

Comments
 (0)