Text box translation into address - c#

I work in an environment where we do a lot of remote troubleshooting. I am developing a C# Console Windows Application that speeds the process of logging into different programs.
It is a multi-tabbed application where on the first Window, there is a text box where the user enters a site number.
What I would like help with is how can I write a code that will read that 3 or 4-digit site number and translate it into an IP address?
In one part of the application, I need it to translate it into an IP address inside a URL. For example, say the user wants to connect to a server at site 1234. They enter 1234 in the text box on the main page and then click connect on the Server tab. I need the site number to implant itself inside a URL looking something like //10.12.34.1:80/.

I'll answer this only for the sake of showing you that your simplified requirements can be easily achieved.
If you only need to decide between 3 and 4 char input values and hardcode the start and end of the target IP address this should do the trick:
string userinput; // 3 or 4 char user input from TextBox
string secondoctet = string.Empty;
string thirdoctet = string.Empty;
if (userinput.Length == 3)
{
secondoctet = userinput.Substring(0, 1);
thirdoctet = userinput.Substring(1, 2);
}
if (userinput.Length == 4)
{
secondoctet = userinput.Substring(0, 2);
thirdoctet = userinput.Substring(2, 2);
}
// using hardcoded IP format template inserting 2nd and 3rd octet into placeholders
string targetip = string.Format("10.{0}.{1}.1", secondoctet, thirdoctet);
Please keep in mind that this solution is restricted to the simple rule that 3-char input will always be split in 1-2 and 4-char input will always be split in 2-2.
As already mentioned in my comment without doubt you will soon face requirements for splitting according to different rules.

Related

How to combine/connect Arabic letters from isolated form to connected form?

I'm trying to combine/connect Arabic letters coming from a string.
The issue with this language and others is that each letter has 4 ways of being displayed, Isolated, Initial, Medial and final.
I've read a lot about the issue but didn't understand if there is a solution or not.
I have this text: مرحبا (which means "Marhaba" - Hello), it's read from right to left.
I'm developing a plug-in for another program which gives me the same string connected but backwards like this: ابحرم (if we read it from LTR it's ok but it should be RTL), so I separate the chars and reverse them, but then when I draw them on screen (graphics app) they are being displayed in their Isolated form like so: م ر ح ب ا reading fine from RTL but not connected.
So I was trying many things to make them display on the screen in the connected form but didn't get it to work, is there a simple method that I could use to connect them when displayed?
Thanks in advance.
My code for reversing the letters is:
TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator(stringFromApp);
List<string> elements = new List<string>();
while (enumerator.MoveNext())
elements.Add(enumerator.GetTextElement());
elements.Reverse();
string reversed = string.Concat(elements);
App.textObject.text = reversed;

Is it safe to encode an Html string before it's inserted in an SQL database and then send it back to the browser

I've been reading extensively for the last 2 days about securing ASP.NET applications. However there are contradictory opinions whether html form inputs should be encoded before they are inserted in a SQL database or just before they are sent back to the browser. I think the former opinion makes more sense to me and I will encode all form inputs once they are received by the server and then inserted in the database. However I'm a bit confused about what will happen next!
So let me follow a string from the time it's created until it is sent back to the browser.
Step 1
A user inputs the following string:
string userString = "It's important to know that 1 > 0";
Step 2
Once received by the server the string is encoded and inserted in the database.
string RenderedString = HttpUtility.HtmlEncode(userString);
As a result the string in the database is saved as follows:
"It's important to know that 1 > 0."
Step 3
I want to send the rendered string back to the browser as it is so it will be written on the webpage as html, and not parsed and rendered by the browser. The result will be:
string StringResult = "It's important to know that 1 > 0";
My question is: Should I add any extra "safety" step before sending the string back to the browser or is it enough with the 3 above steps? Any help would be well appreciated?
I would always save the data without encoding because I might need to show it in some other form e.g: JSON. Saving it encoded to the database would mean to me that I'm putting the view logic inside my data model.
Your assumption (that strings stored in the database are only sent back to the browser) is incorrect.
You can do other things with strings, which do not expect them to be HTML-encoded:
Search for them
Send them to Web Services
Process them in C# or Visual Basic
Use them in PDF files and Excel spreadsheets
Most software assumes strings are unencoded.
MVC automatically HTML-encodes strings before sending them to the browser.

Create Variable from part of file

Hi having trouble working out where to start with creating a variable from a file created by Windows Remote Assistance. I need to extract the port from the text file so i can create an ssh tunnel allowing remote assistance from anywhere.
the port appears after the ip address in 'RCTICKET="65538,1,192.168.9.22:7532,' The colon is the first one in the whole file so I think I need to search for the first ":" and then copy the 4 digits that come after it unless the port is 5 digits ( I think here checking if the 5th character is a comma meaning a 4 digit port of if its a number meaning a 5 digit port )
Any help where to start with this I've been googling for hours just can't think how to put this in a search term.
Below is an example of test.msrcincident the file created by Microsoft Remote Assistance that i need to extract the port from
<?xml version="1.0"?>
<UPLOADINFO TYPE="Escalated"><UPLOADDATA USERNAME="jon" LHTICKET="BDF9C9782B31A1BC276C029A169930ABB4490E2088169FA45A3A095258F5C54D345F4D793363E2C9 B924C5D6A38210AF2E86B3E3D33E5BEB3E35729ECDA88D5F5CE23879899768432726AF419FA2147194F4358BA2A0F245C4307EC8CAB882E2B670977562E5423C90EC336A15BA3DC57496F1EBB26B55B449B45FBD317CD4E422186EA7989F78C6FC3019BCF5831B1E060B174C5254D92448992A543079E576A66617F8B5BEA4C5961FC75C0B67F28B996CD4F1247DBC1C725B9D69B094B53AE24A533501A607CF119ED99C34F0C7210376C6564A48E25871AA32934409D981CF63F60DA956B0877AFBD669DFC321D16D55A34B9949AE0B26B6EEB473915AC416ABFC1129C08021F4011F1F0D1869BB86842C0218C03286C956FC7897B319E0B3A495EBA8ED41835E84E6BAD6B30199F6ACF191B6529DF2C5A264F578AF3B31A84997DA9C4BF1F8AD9E4931F99AE94A0E66D941F050AC0B025523148A95D24E60A6C548341C486BB40089B2088F5FE49AC966D65B728E36E0D7D76C98827335983BEC912DFC0B714DBBBFA060DE62658E7BABDB9BEB45486138950548DA62FDFD6437D0798A67D20CA1911880F58FCDA5F98FA5E0CAEF643171FE9DA8AF046" RCTICKET="65538,1,192.168.9.22:7532,*,U15FphW2EDtpPVdlHmafYLmnO/aVc+YFoFEw30tpjJ+6vJ+LspOTtaqgFoDt3bsp,*,*,P1ooZJPDyfMMTXqlz5hACdwD8F4=" PassStub="TE*0ViGNuB2T6I" RCTICKETENCRYPTED="1" DtStart="1379526042" DtLength="360" L="0"/></UPLOADINFO>
Thank you for reading
Something simple like this would get you the data you need, to some extent:
var reader = XDocument.Load("path to XML file");
var data = reader.Element("UPLOADINFO")
.Element("UPLOADDATA")
.Attribute("RCTICKET")
var values = data.Split(',');
You will need to work with that RCTICKET string to extract the value you need. It would be a bit safer to work with commas, colons, and whatnot in the context of a single attribute instead of the whole file. Caveat: When I generated an incident file, I ended up with multiple IP addresses in the RCTICKET field. I have multiple VPNs and ethernet adapters in my machine. You will have to pick the right one.
You will also want to handle failures if the XML isn't in the format we expect, or if the file is otherwise inaccessible. You can do this with a try/catch and/or checking for nulls.

Extracting data from text using templates

I'm building a web service which receives emails from a number of CRM-systems. Emails typically contain a text status e.g. "Received" or "Completed" as well as a free text comment.
The formats of the incoming email are different, e.g. some systems call the status "Status: ZZZZZ" and some "Action: ZZZZZ". The free text sometimes appear before the status and somethings after. Status codes will be mapped to my systems interpretation and the comment is required too.
Moreover, I'd expect that the the formats change over time so a solution that is configurable, possibly by customers providing their own templates thru a web interface would be ideal.
The service is built using .NET C# MVC 3 but I'd be interested in general strategies as well as any specific libraries/tools/approaches.
I've never quite got my head around RegExp. I'll make a new effort in case it is indeed the way to go. :)
I would go with regex:
First example, if you had only Status: ZZZZZ- like messages:
String status = Regex.Match(#"(?<=Status: ).*");
// Explanation of "(?<=Status: ).*" :
// (?<= Start of the positive look-behind group: it means that the
// following text is required but won't appear in the returned string
// Status: The text defining the email string format
// ) End of the positive look-behind group
// .* Matches any character
Second example if you had only Status: ZZZZZ and Action: ZZZZZ - like messages:
String status = Regex.Match(#"(?<=(Status|Action): ).*");
// We added (Status|Action) that allows the positive look-behind text to be
// either 'Status: ', or 'Action: '
Now if you want to give the possibility to the user to provide its own format, you could come up with something like:
String userEntry = GetUserEntry(); // Get the text submitted by the user
String userFormatText = Regex.Escape(userEntry);
String status = Regex.Match(#"(?<=" + userFormatText + ").*");
That would allow the user to submit its format, like Status:, or Action:, or This is my friggin format, now please read the status -->...
The Regex.Escape(userEntry) part is important to ensure that the user doesn't break your regex by submitting special character like \, ?, *...
To know if the user submits the status value before or after the format text, you have several solutions:
You could ask the user where his status value is, and then build you regex accordingly:
if (statusValueIsAfter) {
// Example: "Status: Closed"
regexPattern = #"(?<=Status: ).*";
} else {
// Example: "Closed:Status"
regexPattern = #".*(?=:Status)"; // We use here a positive look-AHEAD
}
Or you could be smarter and introduce a system of tags for the user entry. For instance, the user submits Status: <value> or <value>=The status and you build the regex by replacing the tags string.

How to compare last 3 char of a input string in asp.net website

I have a text box which gets the link for mp3 files,
Ex: http://www.woo55.com/play/6812/01-till-the-world-ends--(www-songslover-com)-mp3
if the user is entering the above link how can I get the last 3 letters to compare it to find whether it ends with mp3. Can I do it in client side, or is it only possible to do it in server side, I want to do this in asp.net website, my backend is c#.
Assign the link to a string and you can check like this.
if(link.ToLower().EndsWith("mp3"))
{
}
<yourString>.EndsWith("mp3") will return true or false.
You could do this either client side or server side. To do it with Javascript (client side) you could simply concatenate the last three characters of the string and see if it equals what you're looking for:
var link = "http://www.woo55.com/play/6812/01-till-the-world-ends--(www-songslover-com)-mp3";
var lastThreeChars = link.charAt(link.length-3) + link.charAt(link.length-2) + link.charAt(link.length-1);
if(lastThreeChars == "mp3") //do something

Categories