use specific proxy when call api #37049
HASSEN-MEDDEB-ATOS opened this issue · 2 comments
Hello,
i use .NET Core 3.1. I want to make specific api when i call Microsoft graph without using export http_proxy and https_proxy :
public static class CRMConnection
{
private static CdsServiceClient sp = null;
private static IWebProxy proxy = WebRequest.DefaultWebProxy;
private static string Xml_Settings_PathWin = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName + @"\VSMP_SECURE_SETTINGS.xml";
private static string Xml_Settings_PathLin = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.Parent.FullName + @"/VSMP_SECURE_SETTINGS.xml";
private static XmlDocument VSMP_Settings;
public static CdsServiceClient InitCRMConnection()
{
try
{
EnableProxy();
string connectionstring = ConfigurationManager.ConnectionStrings["connectionVSMPSecretkeyVSMP"].ConnectionString;
sp = new CdsServiceClient(connectionstring);
}
catch (Exception ex)
{
LogHelper.Writer("Erreur lors de la connexion au crm, Message de l'erreur : " + Environment.NewLine + ex.ToString());
}
return sp;
}
private static void EnableProxy()
{
VSMP_Settings = new XmlDocument();
if (OperatingSysteme.IsLinux())
{
VSMP_Settings.Load(Xml_Settings_PathLin);
}
else
{
VSMP_Settings.Load(Xml_Settings_PathWin);
}
var _proxyurl = VSMP_Settings.DocumentElement.SelectSingleNode("//VSMP_Proxy_SettingsAtos//ProxyUrl").InnerText;
var _proxyport = Convert.ToInt32(VSMP_Settings.DocumentElement.SelectSingleNode("//VSMP_Proxy_SettingsAtos//ProxyPort").InnerText);
//var _proxylogin = VSMP_Settings.DocumentElement.SelectSingleNode("//VSMP_Proxy_Settings//ProxyLogin").InnerText;
//var _proxypass = VSMP_Settings.DocumentElement.SelectSingleNode("//VSMP_Proxy_Settings//ProxyPass").InnerText;
var _proxyswitch = Convert.ToBoolean(VSMP_Settings.DocumentElement.SelectSingleNode("//VSMP_Proxy_SettingsAtos//ProxySwitch").InnerText);
//proxy bypass connection settings
if (_proxyswitch)
{
const SslProtocols _Tls12 = (SslProtocols)0x00000C00;
const SecurityProtocolType Tls12 = (SecurityProtocolType)_Tls12;
ServicePointManager.SecurityProtocol = Tls12;
var proxy = new WebProxy(_proxyurl, _proxyport);
//proxy.Credentials = new NetworkCredential(_proxylogin, _proxypass);
WebRequest.DefaultWebProxy = proxy;
//HttpWebRequest.DefaultWebProxy = proxy;
}
}
}
i did like this but always i get error
System.AggregateException: One or more errors occurred. (One or more errors occurred. (Unauthorized Http Status Code (401) was expected in the response (Parameter 'response')))
---> System.AggregateException: One or more errors occurred. (Unauthorized Http Status Code (401) was expected in the response (Parameter 'response'))
---> System.ArgumentException: Unauthorized Http Status Code (401) was expected in the response (Parameter 'response')
at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationParameters.CreateFromUnauthorizedResponseCommon(IHttpWebResponse response)
at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationParameters.CreateFromResourceUrlCommonAsync(Uri resourceUrl)
at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationParameters.CreateFromResourceUrlAsync(Uri resourceUrl)
The code that was pasted does not show use of ADAL or MSAL libraries, so I have no idea what that does.
For ADAL support for proxies is done like this: https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/wiki/with-proxy
Closing as it's answered
Feel free to reopen if you disagree