@@ -14,12 +14,13 @@ namespace ZNetCS.AspNetCore.Authentication.Basic
1414 using System ;
1515 using System . Linq ;
1616 using System . Text ;
17+ using System . Text . Encodings . Web ;
1718 using System . Threading . Tasks ;
1819
1920 using Microsoft . AspNetCore . Authentication ;
2021 using Microsoft . AspNetCore . Http ;
21- using Microsoft . AspNetCore . Http . Features . Authentication ;
2222 using Microsoft . Extensions . Logging ;
23+ using Microsoft . Extensions . Options ;
2324 using Microsoft . Net . Http . Headers ;
2425
2526 using ZNetCS . AspNetCore . Authentication . Basic . Events ;
@@ -41,14 +42,45 @@ namespace ZNetCS.AspNetCore.Authentication.Basic
4142 /// </remarks>
4243 public class BasicAuthenticationHandler : AuthenticationHandler < BasicAuthenticationOptions >
4344 {
45+ #region Constants
46+
4447 /// <summary>
4548 /// The scheme name is "Basic".
4649 /// </summary>
47- private const string Scheme = "Basic" ;
50+ private const string Basic = "Basic" ;
51+
52+ #endregion
53+
54+ #region Constructors and Destructors
55+
56+ /// <summary>
57+ /// Initializes a new instance of the <see cref="BasicAuthenticationHandler"/> class.
58+ /// </summary>
59+ /// <param name="options">
60+ /// The options.
61+ /// </param>
62+ /// <param name="logger">
63+ /// The logger.
64+ /// </param>
65+ /// <param name="encoder">
66+ /// The encoder.
67+ /// </param>
68+ /// <param name="clock">
69+ /// The clock.
70+ /// </param>
71+ public BasicAuthenticationHandler ( IOptionsMonitor < BasicAuthenticationOptions > options , ILoggerFactory logger , UrlEncoder encoder , ISystemClock clock ) : base (
72+ options ,
73+ logger ,
74+ encoder ,
75+ clock )
76+ {
77+ }
78+
79+ #endregion
4880
4981 #region Methods
5082
51- /// <inheritdoc />
83+ /// <inheritdoc/>
5284 protected override async Task < AuthenticateResult > HandleAuthenticateAsync ( )
5385 {
5486 // RFC 7230 section 3.2.2
@@ -58,19 +90,19 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
5890 if ( ( authorizationHeaderValues == null ) || ( authorizationHeaderValues . Length == 0 ) )
5991 {
6092 this . Logger . LogDebug ( "'Authorization' header is not present in the request." ) ;
61- return AuthenticateResult . Skip ( ) ;
93+ return AuthenticateResult . NoResult ( ) ;
6294 }
6395
64- var basicAuthorizationHeader = authorizationHeaderValues . FirstOrDefault ( s => s . StartsWith ( Scheme + ' ' , StringComparison . OrdinalIgnoreCase ) ) ;
96+ string basicAuthorizationHeader = authorizationHeaderValues . FirstOrDefault ( s => s . StartsWith ( Basic + ' ' , StringComparison . OrdinalIgnoreCase ) ) ;
6597
6698 // Authorization header is not 'Basic' so there is nothing to do by this middleware
6799 if ( string . IsNullOrEmpty ( basicAuthorizationHeader ) )
68100 {
69101 this . Logger . LogDebug ( "'Authorization' header is not in 'Basic' scheme in the request." ) ;
70- return AuthenticateResult . Skip ( ) ;
102+ return AuthenticateResult . NoResult ( ) ;
71103 }
72104
73- var credentials = basicAuthorizationHeader . Replace ( $ "{ Scheme } ", string . Empty ) . Trim ( ) ;
105+ string credentials = basicAuthorizationHeader . Replace ( $ "{ Basic } ", string . Empty ) . Trim ( ) ;
74106
75107 if ( string . IsNullOrEmpty ( credentials ) )
76108 {
@@ -95,35 +127,21 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
95127 return AuthenticateResult . Fail ( "The credentials delimiter is not present in 'Basic' scheme." ) ;
96128 }
97129
98- var userName = decodedCredentials . Substring ( 0 , delimiterIndex ) ;
99- var password = decodedCredentials . Substring ( delimiterIndex + 1 ) ;
130+ string userName = decodedCredentials . Substring ( 0 , delimiterIndex ) ;
131+ string password = decodedCredentials . Substring ( delimiterIndex + 1 ) ;
100132
101- var context = new ValidatePrincipalContext ( this . Context , this . Options , userName , password ) ;
133+ var context = new ValidatePrincipalContext ( this . Context , this . Scheme , this . Options , userName , password ) ;
102134 return await this . Options . Events . ValidatePrincipal ( context ) ;
103135 }
104136
105- /// <inheritdoc />
106- protected override Task HandleSignInAsync ( SignInContext context )
107- {
108- // Basic authentication have to be resolved on every request.
109- throw new NotSupportedException ( ) ;
110- }
111-
112- /// <inheritdoc />
113- protected override Task HandleSignOutAsync ( SignOutContext context )
114- {
115- // Basic authentication have to be resolved on every request.
116- throw new NotSupportedException ( ) ;
117- }
118-
119- /// <inheritdoc />
120- protected override Task < bool > HandleUnauthorizedAsync ( ChallengeContext context )
137+ /// <inheritdoc/>
138+ protected override Task HandleChallengeAsync ( AuthenticationProperties context )
121139 {
122140 var realmHeader = new NameValueHeaderValue ( "realm" , $ "\" { this . Options . Realm } \" ") ;
123141 this . Response . StatusCode = StatusCodes . Status401Unauthorized ;
124- this . Response . Headers . Append ( HeaderNames . WWWAuthenticate , $ "{ Scheme } { realmHeader } ") ;
142+ this . Response . Headers . Append ( HeaderNames . WWWAuthenticate , $ "{ Basic } { realmHeader } ") ;
125143
126- return Task . FromResult ( true ) ;
144+ return Task . CompletedTask ;
127145 }
128146
129147 #endregion
0 commit comments