Skip to content

Commit 436a90a

Browse files
committed
Merge pull request #290 from kleinron/master
Extend NtlmAuthenticator so that it can also impersonate a user
2 parents 19606c6 + 9cb50cd commit 436a90a

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

RestSharp/Authenticators/NtlmAuthenticator.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,50 @@
1414
// limitations under the License.
1515
#endregion
1616

17+
using System;
18+
using System.Net;
19+
1720
#if FRAMEWORK
1821

1922
namespace RestSharp
2023
{
2124
/// <summary>
22-
/// Tries to Authenticate with the credentials of the currently logged in user
25+
/// Tries to Authenticate with the credentials of the currently logged in user, or impersonate a user
2326
/// </summary>
2427
public class NtlmAuthenticator : IAuthenticator
2528
{
26-
public void Authenticate(IRestClient client, IRestRequest request)
29+
private readonly ICredentials credentials;
30+
31+
/// <summary>
32+
/// Authenticate with the credentials of the currently logged in user
33+
/// </summary>
34+
public NtlmAuthenticator()
35+
: this(CredentialCache.DefaultCredentials)
36+
{
37+
}
38+
39+
/// <summary>
40+
/// Authenticate by impersonation
41+
/// </summary>
42+
/// <param name="username"></param>
43+
/// <param name="password"></param>
44+
public NtlmAuthenticator(string username, string password) : this(new NetworkCredential(username, password))
45+
{
46+
}
47+
48+
/// <summary>
49+
/// Authenticate by impersonation, using an existing <c>ICredentials</c> instance
50+
/// </summary>
51+
/// <param name="credentials"></param>
52+
public NtlmAuthenticator(ICredentials credentials)
53+
{
54+
if (credentials == null) throw new ArgumentNullException("credentials");
55+
this.credentials = credentials;
56+
}
57+
58+
public void Authenticate(IRestClient client, IRestRequest request)
2759
{
28-
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
60+
request.Credentials = credentials;
2961
}
3062
}
3163
}

0 commit comments

Comments
 (0)