Getting Client IP Address in ASP.NET [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
How can I get the client's IP Address? When I use HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] I get something like [::1]:12473
When I use HttpContext.Current.Request.UserHostAddress I keep getting ::1

When you run your code local, you get a ::1 (localhost).
If you publish to Azure, you should get the correct IP address.
You can expand your code by checking for the HTTP_X_FORWARDED_FOR header as well. This returns the correct client IP address when proxies and/or load balancers are involved.
string ipAddress = HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = HttpContext.Request.ServerVariables["REMOTE_ADDR"];
}

Related

Getting Wifi RSSI or signal strength using WlanAPI and IpHlpAPI [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 24 days ago.
Improve this question
I'm trying to recreate the functionality of the wifi icon inside the Windows task bar using this library and C#.
https://github.com/dahall/Vanara
To get the current network interface I am calling GetBestInterface from IpHlpAPI and it seems to be returning the right index. However I'm struggling on how to get the RSSI or signal strength value of the wifi network that's attached to this interface. Using this data I could recreate the wifi icon with 1 bars, 2 bars, 3 bars, ect..
Using WlanAPI I can call WlanGetAvailableNetworkList which returns a list that has the signal strength value for each network. There doesn't seem to be any data to connect these two things, so obviously I'm missing something.
Anyone have any ideas on the approach I should take?
You could try to use WlanQueryInterface function with wlan_intf_opcode_current_connection to query various parameters of a specified interface. You could get a percentage value that represents the signal quality of the network from WLAN_ASSOCIATION_ATTRIBUTES structure.

Get plus(+) operator from C# controller as string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
I need to send a plus(+) operator as string from typescript service to C# Controller. When I debug it, typescript sends as "+", but in C# Controller, the parameter comes as null. The equal sign or negative sign(=,-) are working fine. Just plus sign has a problem. For example if I try to send "13+" plus is recognized as a blank -> "13 " in controller How can I solve it?
+ means space in an URL, so I'm assuming you're assembling an url query string by hand? If so, you need to encode + to %2B.

C# Fetch certain email with subject [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How can I fetch an email, that has the subject I'm looking for in Hotmail using C#.
e.g. I want the emails(body/message) that has the word "Yahoo" in its subject.
Tried using many examples online but they weren't really clear. Thanks
You can connect to your hotmail account using the OpenPop.Net open source library. It has a lot of useful methods to communicate with a POP3 server. There is a lot of useful examples online. A simple code to connect to the POP3 server could work look this:
using(Pop3Client client = new Pop3Client())
{
client.Connect(hotmailHostName, pop3Port, useSsl);
client.Authenticate(username, password, AuthenticationMethod.UsernameAndPassword);
// And here you can use the client.GetMessage() method to get a desired message.
// You can iterate all the messages and check properties on each of them.
}
The hotmailHostName should be "pop3.live.com".
The pop3Port should be 995.
The useSsl should be true.

Shortest way to make web address absolute in c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a string with a web address in it which may or may not include the protocol at the beginning. This is going into the href of a html a tag so needs to include the protocol to avoid the address being treated as a relative address. What's the shortest code to achieve this. Just to be clear, the possible inputs and expected outputs are below.
string url = "www.google.com"; //expected "http://www.google.com"
string url = "google.com"; //expected "http://google.com"
string url = "http://www.google.com"; //expected "http://www.google.com"
string url = "https://www.google.com"; //expected "https://www.google.com"
Update:
To those that want to know what I already tried, it was a couple of if statements checking if the url already started with one of the relevant prefixes and then appending it on if necessary. This is trivial for any c# programmer but doesn't come close to the "shortest way". It worked without any problems but my question is to see what better ways there are of doing it.
You can use UriBuilder class for this.
public static Uri GetUri(this string s)
{
return new UriBuilder(s).Uri;
}
This constructor initializes a new instance of the UriBuilder class with the Fragment, Host, Path, Port, Query, Scheme, and Uri properties set as specified in uri.
If uri does not specify a scheme, the scheme defaults to "http:".
This is going into the href of a html a tag so needs to include the protocol to avoid the address being treated as a relative address
You can simply use // to let the browser know it's an absolute url and not a relative path, the browser will then use http or https appropriately (based on the current page context).
For example:
//www.google.com/
Assuming only http/https protocols are expected:
if (!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
&& !url.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
url = "http://" + url;

want to know more about Dynamic Ip [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
what do we mean by dynamic IP ?
and how we can implement a simple example on it using sockets in vb.net ?
thanks all
Read a book about the IP prtocol and the WIndows doucmentation.
Dynamic IP refers to an IP address that is dynamically assigned to a computer when the computer connects to the physical network ,by means of a DHCP server which dynamically configures the host (with a dynamic IP). The opposite is a STATIC IP which is hard-coded into the computer configuration (i.e. you set it statically).
More is in the documentation. This is a low level beginner question - answering it without a lot of context is hard. Books and Documentation provide the context.
A dynamic IP address is assigned from a DHCP server (Dynamic Host Configuration Protocol).
When your network adapter connects to the network, it asks the DHCP server for a lease on an IP address. The DHCP server returns an IP address which has a limited life time. When the lease expires, the network adapter has to ask for a new lease.
The lifetime of a lease is usually something like two hours or 24 hours, whatever is appropriate for the network. A short lifetime for the lease doesn't mean that you change IP address often, because most of the time you will get a lease for the same IP address that you had before.
For a static IP address the DHCP server isn't involved, you simply enter the IP adress in the network settings.
You can't implement dynamic IP addresses using sockets, because you need to have an IP address already to establish a socket connection.

Categories