Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Custom Authentication in client #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions ServiceNow.Api/ServiceNowClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,29 @@ public class ServiceNowClient : IDisposable
private readonly Options _options;

public ServiceNowClient(
string account,
string username,
string password,
string account,
string username,
string password,
Options? options = null)
: this(
account,
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"))),
options)
{
if (username == null)
{
throw new ArgumentNullException(nameof(username));
}

if (password == null)
{
throw new ArgumentNullException(nameof(password));
}
}

public ServiceNowClient(
string account,
AuthenticationHeaderValue authHeader,
Options? options = null)
{
_options = options ?? new();
Expand All @@ -44,16 +64,6 @@ public ServiceNowClient(
throw new ArgumentNullException(nameof(account));
}

if (username == null)
{
throw new ArgumentNullException(nameof(username));
}

if (password == null)
{
throw new ArgumentNullException(nameof(password));
}

var baseAddress = _options.Environment switch
{
ServiceNowEnvironment.GCC => $"https://{account}.servicenowservices.com",
Expand All @@ -68,7 +78,7 @@ public ServiceNowClient(
Accept = {new MediaTypeWithQualityHeaderValue("application/json")},
}
};
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}")));
_httpClient.DefaultRequestHeaders.Authorization = authHeader;
_logger.LogDebug("Created ServiceNowClient instance.");
}

Expand Down