From cdcbc2d552a275107626d1695d5306217ee22182 Mon Sep 17 00:00:00 2001 From: Ravi Patel Date: Mon, 24 Aug 2020 14:00:25 +0530 Subject: [PATCH] Updated IAuthentication interface to support more types of Authentication. --- AutoUpdater.NET/AutoUpdater.cs | 5 +---- AutoUpdater.NET/BasicAuthentication.cs | 9 ++++++++- AutoUpdater.NET/CustomAuthentication.cs | 14 +++++++++----- AutoUpdater.NET/IAuthentication.cs | 5 +++++ 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/AutoUpdater.NET/AutoUpdater.cs b/AutoUpdater.NET/AutoUpdater.cs index 05f08ee4..bf3bb67f 100644 --- a/AutoUpdater.NET/AutoUpdater.cs +++ b/AutoUpdater.NET/AutoUpdater.cs @@ -674,10 +674,7 @@ internal static MyWebClient GetWebClient(Uri uri, IAuthentication basicAuthentic } else { - if (basicAuthentication != null) - { - webClient.Headers[HttpRequestHeader.Authorization] = basicAuthentication.ToString(); - } + basicAuthentication?.Apply(ref webClient); webClient.Headers[HttpRequestHeader.UserAgent] = HttpUserAgent; } diff --git a/AutoUpdater.NET/BasicAuthentication.cs b/AutoUpdater.NET/BasicAuthentication.cs index 2c52397c..b9ff985f 100644 --- a/AutoUpdater.NET/BasicAuthentication.cs +++ b/AutoUpdater.NET/BasicAuthentication.cs @@ -1,4 +1,5 @@ using System; +using System.Net; using System.Text; namespace AutoUpdaterDotNET @@ -13,7 +14,7 @@ public class BasicAuthentication : IAuthentication private string Password { get; } /// - /// Initializes credentials for Basic Authentication. + /// Initializes credentials for Basic Authentication. /// /// Username to use for Basic Authentication /// Password to use for Basic Authentication @@ -29,5 +30,11 @@ public override string ToString() var token = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{Username}:{Password}")); return $"Basic {token}"; } + + /// + public void Apply(ref MyWebClient webClient) + { + webClient.Headers[HttpRequestHeader.Authorization] = ToString(); + } } } diff --git a/AutoUpdater.NET/CustomAuthentication.cs b/AutoUpdater.NET/CustomAuthentication.cs index b58db8a4..d737d968 100644 --- a/AutoUpdater.NET/CustomAuthentication.cs +++ b/AutoUpdater.NET/CustomAuthentication.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Net; namespace AutoUpdaterDotNET { @@ -12,18 +10,24 @@ public class CustomAuthentication : IAuthentication private string HttpRequestHeaderAuthorizationValue { get; } /// - /// Initializes authorization header value for Custom Authentication + /// Initializes authorization header value for Custom Authentication /// /// Value to use as http request header authorization value public CustomAuthentication(string httpRequestHeaderAuthorizationValue) { HttpRequestHeaderAuthorizationValue = httpRequestHeaderAuthorizationValue; } - + /// public override string ToString() { return HttpRequestHeaderAuthorizationValue; } + + /// + public void Apply(ref MyWebClient webClient) + { + webClient.Headers[HttpRequestHeader.Authorization] = ToString(); + } } } diff --git a/AutoUpdater.NET/IAuthentication.cs b/AutoUpdater.NET/IAuthentication.cs index 90d6d7ed..68dd58ab 100644 --- a/AutoUpdater.NET/IAuthentication.cs +++ b/AutoUpdater.NET/IAuthentication.cs @@ -6,5 +6,10 @@ /// public interface IAuthentication { + /// + /// Apply the authentication to webclient. + /// + /// WebClient for which you want to use this authentication method. + void Apply(ref MyWebClient webClient); } }