@@ -38,6 +38,48 @@ public HttpBasicAuthenticator(AuthHeader authHeader)
3838 _authHeader = authHeader ;
3939 }
4040
41+ /// <summary>
42+ /// Initializes a new instance of the <see cref="HttpBasicAuthenticator" /> class.
43+ /// </summary>
44+ /// <param name="credentials">The credentials to use for preauthentication</param>
45+ public HttpBasicAuthenticator ( NetworkCredential credentials )
46+ : this ( credentials , AuthHeader . Www )
47+ {
48+ }
49+
50+ /// <summary>
51+ /// Initializes a new instance of the <see cref="HttpBasicAuthenticator" /> class.
52+ /// </summary>
53+ /// <param name="credentials">The credentials to use for preauthentication</param>
54+ /// <param name="authHeader">Authentication/Authorization header type</param>
55+ public HttpBasicAuthenticator ( NetworkCredential credentials , AuthHeader authHeader )
56+ : this ( credentials . UserName , credentials . Password , authHeader )
57+ {
58+ _authCredential = credentials ;
59+ }
60+
61+ /// <summary>
62+ /// Initializes a new instance of the <see cref="HttpBasicAuthenticator" /> class.
63+ /// </summary>
64+ /// <param name="userName">The user name to be used for authentication</param>
65+ /// <param name="password">The password to be used for authentication</param>
66+ public HttpBasicAuthenticator ( string userName , string password )
67+ : this ( userName , password , AuthHeader . Www )
68+ {
69+ }
70+
71+ /// <summary>
72+ /// Initializes a new instance of the <see cref="HttpBasicAuthenticator" /> class.
73+ /// </summary>
74+ /// <param name="userName">The user name to be used for authentication</param>
75+ /// <param name="password">The password to be used for authentication</param>
76+ /// <param name="authHeader">Authentication/Authorization header type</param>
77+ public HttpBasicAuthenticator ( string userName , string password , AuthHeader authHeader )
78+ {
79+ _authToken = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( $ "{ userName } :{ password } ") ) ;
80+ _authHeader = authHeader ;
81+ }
82+
4183 /// <summary>
4284 /// Gets a value indicating whether the authenticator already as an authorization token available for pre-authentication.
4385 /// </summary>
@@ -88,16 +130,19 @@ public Task PreAuthenticate(IRestClient client, IRestRequest request, ICredentia
88130 /// <returns>The task the authentication is performed on</returns>
89131 public Task PreAuthenticate ( IHttpClient client , IHttpRequestMessage request , ICredentials credentials )
90132 {
91- return Task . Factory . StartNew ( ( ) =>
133+ if ( ! HasAuthorizationToken )
92134 {
93- if ( ! CanPreAuthenticate ( client , request , credentials ) )
94- {
95- throw new InvalidOperationException ( ) ;
96- }
97-
98- var authHeaderValue = $ "{ AuthenticationMethod } { _authToken } ";
99- request . SetAuthorizationHeader ( _authHeader , authHeaderValue ) ;
100- } ) ;
135+ throw new InvalidOperationException ( ) ;
136+ }
137+
138+ var authHeaderValue = $ "{ AuthenticationMethod } { _authToken } ";
139+ request . SetAuthorizationHeader ( _authHeader , authHeaderValue ) ;
140+
141+ #if ASYNC_PCL
142+ return Task . FromResult ( 0 ) ;
143+ #else
144+ return new Task ( ( ) => { } ) ;
145+ #endif
101146 }
102147
103148 /// <summary>
@@ -151,17 +196,20 @@ public virtual bool CanHandleChallenge(IHttpClient client, IHttpRequestMessage r
151196 /// <returns>Task where the handler for a failed authentication gets executed</returns>
152197 public Task HandleChallenge ( IHttpClient client , IHttpRequestMessage request , ICredentials credentials , IHttpResponseMessage response )
153198 {
154- return Task . Factory . StartNew ( ( ) =>
199+ if ( ! CanHandleChallenge ( client , request , credentials , response ) )
155200 {
156- if ( ! CanHandleChallenge ( client , request , credentials , response ) )
157- {
158- throw new InvalidOperationException ( ) ;
159- }
160-
161- var responseUri = client . GetRequestUri ( request , response ) ;
162- _authCredential = credentials . GetCredential ( responseUri , AuthenticationMethod ) ;
163- _authToken = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( $ "{ _authCredential . UserName } :{ _authCredential . Password } ") ) ;
164- } ) ;
201+ throw new InvalidOperationException ( ) ;
202+ }
203+
204+ var responseUri = client . GetRequestUri ( request , response ) ;
205+ _authCredential = credentials . GetCredential ( responseUri , AuthenticationMethod ) ;
206+ _authToken = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( $ "{ _authCredential . UserName } :{ _authCredential . Password } ") ) ;
207+
208+ #if ASYNC_PCL
209+ return Task . FromResult ( 0 ) ;
210+ #else
211+ return new Task ( ( ) => { } ) ;
212+ #endif
165213 }
166214 }
167215}
0 commit comments