-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#33 Add custom header extractor to ASPNETCORE sample.
- Loading branch information
David Lievrouw
committed
Mar 7, 2021
1 parent
b58035e
commit 6d2272c
Showing
2 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/AspNetCoreWebApplication/CustomHeaderSupportingAuthenticationHeaderExtractor.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,40 @@ | ||
using System; | ||
using System.Net.Http.Headers; | ||
using Dalion.HttpMessageSigning.Verification.AspNetCore; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace WebApplication { | ||
public class CustomHeaderSupportingAuthenticationHeaderExtractor : IAuthenticationHeaderExtractor { | ||
private readonly string _headerName; | ||
|
||
public CustomHeaderSupportingAuthenticationHeaderExtractor(string headerName = "Authorization") { | ||
_headerName = headerName ?? throw new ArgumentNullException(nameof(headerName)); | ||
} | ||
|
||
public AuthenticationHeaderValue Extract(HttpRequest request) { | ||
if (request == null) throw new ArgumentNullException(nameof(request)); | ||
|
||
var authHeader = request.Headers[_headerName]; | ||
if (authHeader == StringValues.Empty) { | ||
return null; | ||
} | ||
|
||
var rawAuthHeader = (string) authHeader; | ||
var separatorIndex = rawAuthHeader.IndexOf(' '); | ||
if (separatorIndex < 0) { | ||
return new AuthenticationHeaderValue(rawAuthHeader); | ||
} | ||
|
||
var authScheme = rawAuthHeader.Substring(0, separatorIndex); | ||
|
||
if (separatorIndex >= rawAuthHeader.Length - 1) { | ||
return new AuthenticationHeaderValue(authScheme); | ||
} | ||
|
||
var authParam = rawAuthHeader.Substring(separatorIndex + 1); | ||
|
||
return new AuthenticationHeaderValue(authScheme, authParam); | ||
} | ||
} | ||
} |
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