Is there a way to Http Authentication with WebSocket4Net? I would like to pass credentials to my client. This is what my code looks like:
public bool Connect(string uri, ICredentials credentials = null)
{
if (this.ws == null)
{
try
{
////this.ws = new WebSocket(uri, string.Empty, WebSocketVersion.None, null, );
this.ws = new WebSocket(uri);
this.ws.Opened += this.OnWebSocketOpen;
this.ws.Closed += this.OnWebSocketClose;
this.ws.Error += this.OnWebSocketFail;
this.ws.MessageReceived += this.OnWebSocketMessage;
try
{
this.ws.Open();
return true;
}
catch (Exception ex)
{
Log.Error("Open web socket failed", ex);
this.ws = null;
}
}
catch (Exception exception)
{
throw new CanNotConnectToDeviceException(new Uri(uri, UriKind.Relative), exception);
}
}
return false;
}
I'm not sure that I understand your question clearly, but I still try to help.
Do you mean you're developing a web service, and need "Basic HTTP Authentication"?
If you're doing the server side, and require an authentication, you can response HTTP 401 first.
Most of browser will notify user when it received HTTP 401.
If you're doing the client side, and you want to send authentication automatically,
you can refer to the issue of WebSocket4Net.
Please notice that Basic HTTP Authentication is PLAIN TEXT.
Thanks J.C for the Link. I used it to create a little helper class.
public class HttpHelper : IHttpHelper
{
public KeyValuePair<string, string>
CreateAuthorizationHeader(ICredentials credentials)
{
NetworkCredential networkCredential =
credentials.GetCredential(null, null);
string userName = networkCredential.UserName;
string userPassword = networkCredential.Password;
string authInfo = userName + ":" + userPassword;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
return new KeyValuePair<string, string>("Authorization", "Basic " + authInfo);
}
}
Related
A thousand apologies if this question has been posted already, but I am getting a "The remote server returned an error: (401) Unauthorized" error when attempting to upload a string to a remote server via a web request in SSIS (Visual Studio) by means of a script task - the external company to whom I am sending the data are certain that my public IP and the user credentials I am using to authenticate all have access to the server.
My code looks like this;
public void Main()
{
string proxyServer = (string)Dts.Variables["$Project::ProxyServer"].Value;
int proxyPort = (int)Dts.Variables["$Project::ProxyPort"].Value;
string username = Convert.ToString(Dts.Variables["$Project::UserName"].Value);
string password = Convert.ToString(Dts.Variables["$Project::Password"].GetSensitiveValue());
Uri TargetSite = new Uri((string)Dts.Variables["$Project::TargetSite"].Value);
string datatosend = (string)Dts.Variables["User::RequestBody"].Value;
if (datatosend != "")
{
string result;
using (WebClient webClient = new WebClient())
{
webClient.Proxy = new WebProxy(proxyServer, proxyPort); // Connect via the proxy
var bytes = Encoding.UTF8.GetBytes(username + ":" + password);// Build a means to authenticate
var auth = "Basic " + Convert.ToBase64String(bytes);
NetworkCredential myCreds = new NetworkCredential(username, password);
webClient.Credentials = myCreds;
webClient.Headers[HttpRequestHeader.Authorization] = string.Format("Basic ", credentials);
//webClient.Headers[HttpRequestHeader.Authorization] = auth; // Add the authorization header
webClient.Headers[HttpRequestHeader.ContentType] = "text/json"; // Add information about the content
ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
try
{
result = webClient.UploadString(TargetSite, datatosend); // Issue the request
}
catch (WebException ex)
{
Dts.Events.FireError(0, "", "UnableToSendData: " + ex.Message.ToString() + ex.StackTrace, string.Empty, 0);
return;
}
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
Everything works until the request is issued - that's when I am hit with the 401 error and the response from the Uri is null;
I am at a loss because I am told my public IP is on their permissions list, so not sure why the request fails.
Any assistance would be greatly appreciated.
Many thanks in advance.
I am trying to validate an email whether it is real or fake. I am using some code but it always returns Ok.
This is my code:
public string validateEmail(string userEmail)
{
try
{
string key = "xxxx";
string email = "xxxx";
var crmUrl = "https://app.emaillistvalidation.com/api/verifEmail?secret=" + key + "&email=" + email;
string url = crmUrl;
var client = new RestClient(url);
client.RemoteCertificateValidationCallback = new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });
var request = new RestRequest();
request.AddHeader("authorization", "Basic xxxxxxxxxxxxxxxxxx");
IRestResponse response = client.Execute(request);
var content = response.Content;
return content;
}
catch (Exception ex)
{
}
return default;
}
What is wrong here? And what can I do instead?
I am trying to validate an email whether it is real or fake. I am
using some code but it always returns Ok.
I have gone through the code and the API you have shared. I also register and open a demo api on https://app.emaillistvalidation.com/api and it's behaving as expected for both valid and invalid email. Here are my steps:
Register on app.emaillistvalidation.com
My Controller Code
[Route("ValidateEmail")]
[HttpGet]
public async Task<IActionResult> ValidateEmail(string emailId)
{
var handler = new HttpClientHandler();
var data = "";
handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Accept.Clear();
var responseFromApi = await client.GetAsync("https://app.emaillistvalidation.com/api/verifEmail?secret=" + apiSecret + "&email=" + emailId + "");
if (responseFromApi.IsSuccessStatusCode)
{
data = await responseFromApi.Content.ReadAsStringAsync();
}
return Ok(data);
}
Output For Valid & Invalid Email
Postman Direct Test
I have also tested on Postman for valid email I got ok response and for invalid and wrong email I got unknown and disable response:
Valid Email:
Wrong Email:
Wrong Domain:
Note: Please double-check the way you are trying. You could have a look on my code and the way I have tested. It's working as expected which has been shared in the screenshots
I have create 1 service and the purpose of this service is to get value from hardware and send to the server (MVC,Controller) and display the value.
The value sometime able to send to server but sometime show this error msg.
THIS ERROR MSG GET FROM SERVICE
13/8/2019 5:35:37 PM : The remote server returned an error: (400) Bad
Request.. at System.Net.WebClient.UploadDataInternal(Uri address,
String method, Byte[] data, WebRequest& request) at
System.Net.WebClient.UploadString(Uri address, String method, String
data) at IndicatorReadingService.Service1.Start()
I don't know what mistake that i have make.Hope you guys can helped me solved this problem thank you in advance.
protected override void OnStart(string[] args)
{
string sInterval = ConfigurationManager.AppSettings["Interval"];
int iInterval;
if(Int32.TryParse(sInterval,out iInterval))
{
iInterval = Int32.Parse(sInterval);
}
else
{
iInterval = 700;
}
timer.Interval = iInterval;
timer.Elapsed += new ElapsedEventHandler(this.OnElapsedTime);
timer.Enabled = true;
CreateLogFile(DateTime.Now.ToString() + " : Start Service.");
}
//POST
private void Start()
{
data = "NULL";
try
{
data = new Indicator().StartRead();
//var vm = new { reading = temp};
using (var client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
client.Encoding = Encoding.UTF8;
client.UploadString(new Uri(ConfigurationManager.AppSettings["APIUrl"] + data), "POST");
}
}
catch (Exception ex)
{
using (var client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
client.Encoding = Encoding.UTF8;
client.UploadString(new Uri(ConfigurationManager.AppSettings["APIUrl"] + ex.Message), "POST");
}
CreateLogFile(DateTime.Now.ToString() + " : " + ex.Message+"."+ex.StackTrace);
}
}
client.UploadString is expecting the first parameter to be the URL you are wanting to send it to, and the second to be the data. You should change your code to be:
client.UploadString(ConfigurationManager.AppSettings["APIUrl"], data);
Good practices
How to create a rest api function which ask for
Headers, and body, and what content is going to set in header and what
content is going to be in body?
and how to call this function? (programmatically)
Assuming that you are using a WebClient to access the REST resource in question, you can pass the WebClient and set its headers in a method like this:
public static void AddBasicAuthRequestHeaders(WebClient webClient, string userId, string password)
{
webClient.Headers.Add("Content-Type", "application/json");
webClient.Headers.Add("Accept", "application/json");
webClient.Headers.Add("Authorization", "Basic " + Helper.Base64EncodeString(string.Format("{0}:{1}", userId, password)) + "");
}
Since the request body is passed to the WebClient when it is used, you don't need to pass a reference to the client in the method that builds out the request body.
public static string BuildRequestBody(){
return "{\"Value\":\"?$filter=Name eq '" + objectName + "'&$select=Id\"}";}
Then you can use them like so:
using (WebClient webClient = new WebClient())
{
bool isSuccessful = false;
string requestBody = BuildRequestBody();
AddBasicAuthRequestHeaders(webClient, "myUser", "myPwd");
try
{
string responseString = webClient.UploadString(resourceUrl, requestBody);
JArray responseArray = JArray.Parse(responseString);
isSuccessful = (bool)responseArray[0]["IsSuccessful"];
if (isSuccessful)
{
objectId = (int)responseArray[0]["RequestedObject"]["Id"];
}
}
catch (Exception ex)
{
string logMessage = string.Format("Error retrieving Id for object {0}. Exception: {1}\r\nDetails:{2}", objectName, ex.Message, ex.StackTrace);
Helper.Log(LogLevel.Error, logMessage);
throw (ex);
}
}
return objectId;
Can someone please take a look at the code below and tell me what I am doing wrong. I am just going in circles,,, any pointers greatly appreciated
public class FtpWebRequestUtil
{
private static string RemoteHost;
private static string RemoteFtpPath;
public static NetworkCredential Credential = new NetworkCredential();
public FtpWebRequestUtil()
{
}
public FtpWebRequestUtil(string RemoteAddress, string RemotePath, string RemoteUser, string RemotePwd)
{
Credential.UserName = RemoteUser;
Credential.Password = RemotePwd;
RemoteHost = RemoteAddress;
RemoteFtpPath = RemotePath;
}
public string UploadFile(string localFilePath)
{
int startTime = Environment.TickCount;
// Console.WriteLine("Uploading File " + localFilePath);
try
{
FileInfo localFile = new FileInfo(localFilePath); //e.g.: c:\\Test.txt
byte[] buf = new byte[2048];
int iWork;
string remoteFile = "ftp://" + RemoteHost + "/" + RemoteFtpPath + "/" + localFile.Name;
FtpWebRequest req = (FtpWebRequest) FtpWebRequest.Create(remoteFile);
// req.Proxy =
req.Credentials = Credential;
// FtpWebRequest req = (FtpWe
req.UseBinary = true;
req.KeepAlive = true;
req.Method = WebRequestMethods.Ftp.UploadFile;
StreamWriter myStreamWriter = new StreamWriter(req.GetRequestStream());
myStreamWriter.Write(new StreamReader("TestFiles\\" + localFile.Name).ReadToEnd());
myStreamWriter.Close();
FtpWebResponse myFtpWebResponse = (FtpWebResponse) req.GetResponse();
Console.WriteLine("Upload File Complete, status: " + myFtpWebResponse.StatusDescription);
myFtpWebResponse.Close();
return "SUCCESS";
}
catch (Exception ex)
{
Console.WriteLine("There was an error connecting to the FTP Server.");
Console.WriteLine(ex.Message);
throw ex;
}
Console.WriteLine("Time taken for downloading file is " + (Environment.TickCount - startTime).ToString());
return "FAILURE";
}
************************ *********************************
FtpWebRequestUtil ftpClient = new FtpWebRequestUtil(FtpUrl, InputFolder, FtpUser, FtpPassword);
try
{
Thread.Sleep(5000);
ftpClient.UploadFile(UploadingFileName);
}
catch (Exception exception)
{
Assert.Fail(exception.Message);
}
finally
{
ftpClient = null;
}
}
}
req.Proxy = new WebProxy(); // initialize this FtpWebRequest property
It turns out that only the RETR, LIST, and NLST methods are supported by System.Net.FtpWebRequest when a HTTP proxy is configured and it doesn't matter that you are not setting a proxy in your code: if a HTTP proxy (not FTP proxy) is configured in the system proxy settings (in i.e. : Internet Options\Connections\LAN setting\Proxy Server\ Use a proxy server for your LAN), then you will get this error when trying to upload to the FTP server.
The workaround is use IE to change the system settings to switch off the use of the HTTP proxy. However if you have access to the affected code the solution is to set the Proxy property of the request to null, for example:
request.Proxy = null;
The exceptions itself is the answer - it is not supported. Probably you have some HTTP proxy that is preventing direct connection to FTP. According to MS documentation, if the specified proxy is an HTTP proxy, only the DownloadFile, ListDirectory, and ListDirectoryDetails commands are supported - so UploadFile is not.