Skip to content

Commit aed8268

Browse files
author
Claudia Beatriz Murialdo Garrone
committed
Fix: Ensure Basic Authentication adds the Authorization header correctly.
1 parent 98aca03 commit aed8268

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,10 @@ public void AddAuthentication(int scheme, string realm, string user, string pass
488488
{
489489
if (scheme >= _Basic && scheme <= _Kerberos)
490490
_authCollection.Add(new GxAuthScheme(scheme, realm, user, password));
491+
if (scheme == _Basic) {
492+
string authHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{user}:{password}"));
493+
AddHeader(HttpHeader.AUTHORIZATION, new AuthenticationHeaderValue("Basic", authHeader).ToString());
494+
}
491495
}
492496

493497
public void AddProxyAuthentication(int scheme, string realm, string user, string password)
@@ -727,7 +731,7 @@ void setContentHeaders(HttpRequestMessage request, string contentType)
727731
}
728732
InferContentType(contentType, request);
729733
}
730-
void setHeaders(HttpRequestMessage request, CookieContainer cookies, out string contentType)
734+
internal void SetHeaders(HttpRequestMessage request, CookieContainer cookies, out string contentType)
731735
{
732736
HttpRequestHeaders headers = request.Headers;
733737
contentType = null;
@@ -864,7 +868,7 @@ HttpResponseMessage ExecuteRequest(string method, string requestUrl, bool contex
864868
RequestUri = new Uri(requestUrl),
865869
Method = new HttpMethod(method),
866870
};
867-
setHeaders(request, cookies, out string contentType);
871+
SetHeaders(request, cookies, out string contentType);
868872
SetHttpVersion(request);
869873
bool disposableInstance = true;
870874
try
@@ -926,7 +930,7 @@ async Task<HttpResponseMessage> ExecuteRequestAsync(string method, string reques
926930
RequestUri = new Uri(requestUrl),
927931
Method = new HttpMethod(method),
928932
};
929-
setHeaders(request, cookies, out string contentType);
933+
SetHeaders(request, cookies, out string contentType);
930934
SetHttpVersion(request);
931935
bool disposableInstance = true;
932936
try
@@ -1581,6 +1585,8 @@ static ICredentials getCredentialCache(Uri URI, ArrayList authenticationCollecti
15811585
}
15821586
else
15831587
{
1588+
//return new NetworkCredential(auth.User, auth.Password);
1589+
15841590
if (cc == null)
15851591
{
15861592
cc = new CredentialCache();

dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class HttpHeader
5252
internal static string TRANSFER_ENCODING = "Transfer-Encoding";
5353
internal static string X_CSRF_TOKEN_HEADER = "X-XSRF-TOKEN";
5454
internal static string X_CSRF_TOKEN_COOKIE = "XSRF-TOKEN";
55+
internal static string AUTHORIZATION = "Authorization";
5556
}
5657
internal class HttpHeaderValue
5758
{

dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
using System.Linq;
55
using System.Net;
66
using System.Net.Http;
7+
using System.Text;
78
using System.Threading.Tasks;
89
using GeneXus.Application;
910
using GeneXus.Http.Client;
1011
using Xunit;
12+
13+
14+
1115
#if !NETCORE
1216
using System.Web.SessionState;
1317
using System.Web;
@@ -18,8 +22,10 @@ namespace xUnitTesting
1822

1923
public class GxHttpClientTest
2024
{
21-
const int MAX_CONNECTIONS= 5;
22-
public GxHttpClientTest() {
25+
const int MAX_CONNECTIONS = 5;
26+
27+
public GxHttpClientTest()
28+
{
2329
Environment.SetEnvironmentVariable("GX_HTTPCLIENT_MAX_PER_ROUTE", MAX_CONNECTIONS.ToString(), EnvironmentVariableTarget.Process);
2430
}
2531
[Fact]
@@ -203,5 +209,31 @@ public void NoStoreHeader()
203209
Assert.Equal(0, result);
204210
}
205211
#endif
212+
213+
#if NETCORE
214+
[Fact]
215+
public void BasicAuthenticationIncludesHeader()
216+
{
217+
GxContext context = new GxContext();
218+
using (GxHttpClient httpclient = new GxHttpClient(context))
219+
{
220+
string url= "https://www.google.com/";
221+
string username = "user";
222+
string password = "pass";
223+
string credentialsBase64 = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
224+
httpclient.AddAuthentication(0, string.Empty, username, password);
225+
226+
HttpRequestMessage request = new HttpRequestMessage()
227+
{
228+
RequestUri = new Uri(url),
229+
Method = HttpMethod.Post,
230+
};
231+
httpclient.SetHeaders(request, null, out string contentType);
232+
string headerValue = request.Headers.GetValues("Authorization").FirstOrDefault();
233+
Assert.Equal(headerValue, $"Basic {credentialsBase64}");
234+
}
235+
}
236+
#endif
206237
}
238+
207239
}

0 commit comments

Comments
 (0)