-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- remove policy with multiple schemes sample tests and codes - remove mode
- Loading branch information
Showing
12 changed files
with
165 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
authentication/MultiAuthentication/Controllers/SchemeSelectorController.cs
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
authentication/MultiAuthentication/Handlers/AlternativeAuthenticationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.Options; | ||
using System.Security.Claims; | ||
using System.Text.Encodings.Web; | ||
|
||
namespace MultiAuthentication.Handlers; | ||
|
||
public class AlternativeAuthenticationHandler( | ||
IOptionsMonitor<AuthenticationSchemeOptions> options, | ||
ILoggerFactory logger, | ||
UrlEncoder encoder | ||
) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder) | ||
{ | ||
protected override Task<AuthenticateResult> HandleAuthenticateAsync() | ||
{ | ||
if (!Context.Request.Headers.TryGetValue("X-Alternative", out var value)) | ||
{ | ||
return Task.FromResult(AuthenticateResult.NoResult()); | ||
} | ||
|
||
if (value.ToString() != "Alternative") | ||
{ | ||
return Task.FromResult(AuthenticateResult.Fail("Invalid token value")); | ||
} | ||
|
||
var principal = new ClaimsPrincipal(new ClaimsIdentity("Alternative")); | ||
if (Context.Request.Headers.TryGetValue("X-Claim", out var claim)) | ||
{ | ||
((ClaimsIdentity?)principal.Identity)?.AddClaim(new($"{claim}", $"{claim}")); | ||
} | ||
|
||
return Task.FromResult(AuthenticateResult.Success(new(principal, "Alternative"))); | ||
} | ||
} |
14 changes: 2 additions & 12 deletions
14
...n/Handlers/ApiKeyAuthenticationHandler.cs → ...andlers/AnonymousAuthenticationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,17 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Extensions.Primitives; | ||
using System.Security.Claims; | ||
using System.Text.Encodings.Web; | ||
|
||
namespace MultiAuthentication.Handlers; | ||
|
||
public class ApiKeyAuthenticationHandler( | ||
public class AnonymousAuthenticationHandler( | ||
IOptionsMonitor<AuthenticationSchemeOptions> options, | ||
ILoggerFactory logger, | ||
UrlEncoder encoder | ||
) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder) | ||
{ | ||
protected override Task<AuthenticateResult> HandleAuthenticateAsync() | ||
{ | ||
if (Context.Request.Headers.TryGetValue("X-API-KEY", out StringValues key)) | ||
{ | ||
var claim = new Claim("Token", $"{key}"); | ||
var principal = new ClaimsPrincipal(new ClaimsIdentity([claim], "ApiKey")); | ||
|
||
return Task.FromResult(AuthenticateResult.Success(new(principal, "ApiKey"))); | ||
} | ||
|
||
{ | ||
return Task.FromResult(AuthenticateResult.NoResult()); | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
authentication/MultiAuthentication/Handlers/BearerTokenAuthenticationHandler.cs
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
authentication/MultiAuthentication/Handlers/DefaultAuthenticationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.Options; | ||
using System.Security.Claims; | ||
using System.Text.Encodings.Web; | ||
|
||
namespace MultiAuthentication.Handlers; | ||
|
||
public class DefaultAuthenticationHandler( | ||
IOptionsMonitor<AuthenticationSchemeOptions> options, | ||
ILoggerFactory logger, | ||
UrlEncoder encoder | ||
) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder) | ||
{ | ||
protected override Task<AuthenticateResult> HandleAuthenticateAsync() | ||
{ | ||
if (!Context.Request.Headers.TryGetValue("X-Default", out var value)) | ||
{ | ||
return Task.FromResult(AuthenticateResult.NoResult()); | ||
} | ||
|
||
if (value.ToString() != "Default") | ||
{ | ||
return Task.FromResult(AuthenticateResult.Fail("Invalid token value")); | ||
} | ||
|
||
var principal = new ClaimsPrincipal(new ClaimsIdentity("Default")); | ||
if (Context.Request.Headers.TryGetValue("X-Claim", out var claim)) | ||
{ | ||
((ClaimsIdentity?)principal.Identity)?.AddClaim(new($"{claim}", $"{claim}")); | ||
} | ||
|
||
return Task.FromResult(AuthenticateResult.Success(new(principal, "Default"))); | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
authentication/MultiAuthentication/Handlers/OrganizationIdAuthenticationHandler.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.