HttpContext.Current.Request.Url.Authority is getting cached by IIS - c#

I have a web application (asp.net, C#) running for more than 2 months for internal employees of my company. The application was accessible by live IP address like 121.XXX.XX.200.
In my code-base, I used "HttpContext.Current.Request.Url.Authority" to get this particular IP address. This was also working fine till day before yesterday.
However, yesterday the code "HttpContext.Current.Request.Url.Authority" started giving me local IP address of the server like "10.X.XXX.20" instead of the live one.
Now I was able to reproduce the error. Whenever I browse the application using the local IP address, IIS seems to be caching the domain name. Now when I browse the same application using live IP address from different computer. It still gives me the local IP.
What can be the solution to this?

Try using HttpContext.Current.Request.Url.Host instead of HttpContext.Current.Request.Url.Authority.
As long as I remember Authority gets the DNS domain name of host which can cause problems sometimes.

Related

How to get hostname or unique identification of computers?

We have an app (ASP.Net 3.1 Core MVC,SQL Server) that is deployed internally (network) wherein we can only access the app by using AnyConnect VPN. It is also given that we have a remote connection in an onsite computer.
Can you give me an idea or suggestions on how to identify the client if he/she is working at home or at the office. I tried searching here in SO on how to get Hostname , it works on the physically connected to the network computer at the office but it does not work when the app is accessed externally (through VPN).
Any suggestions would be greatly appreciated !

Sharing a folder fails due to WINS name resolution

I am trying to set up a shared folder with an old Win CE 5.0 device I am using to learn and improve my C# skills.
Possible options to do this can be done via console: net use sharedfolder \\mypc /user:foo /password:bar, to start an extra process from my C# application which then runs the console with the previous parameters or by using the function WNetAddConnection3.
However, I receive every time Error 53, ERROR_BAD_NETWORK_PATH.
I have checked the connection, as well as the path - always error 53.
A connection between another PC and \\mypc works fine without any problems. By comparison of my PC's network connection with the Win CE 5.0 device I have found out that there is only one difference: the Win CE 5.0 device has one WINS-server less. After setting the IP profile manually (IP Address, Subnet Mask, Default Gateway, DHCP, WINS), as well as adding the missing WINS-server, the problem disappears and the folder has been mounted successfully.
How can I fix this problem without changing the IP profile by hand?
In a normal network the WINS will be set by the DHCP server. The WINS entry of a network connection can also be set manually.
The problem with network sharing on Windows Mobile (WM) and Windows CE (WCE) is that it does not allow IP addresses, it only accepts windows network names. If you do not have WINS, a netbios network name can be resolved by the network card using a hosts entry, a broadcast or a WINS server.
As you do not have access to the DHCP settings of ActiveSync, you can either set WINS manually or by code or simply add a host entry for the connected PC. The activesync connected PC will can act as WINS server or , when you enter the netbios name for the netbios share server, the device can simply translate the netbios name to an IP. So either use the activesynced PC or the netbios server IP address as a WINS entry or add the name and IP of the sharing PC to the WCE/WM hosts table (registry: http://windowsmobilepro.blogspot.de/2006/04/etchosts-file-equivalent-in-windows.html).
[HKEY_LOCAL_MACHINE\Comm\Tcpip\Hosts\hello]
"ipaddr"=hex:a1,a3,a5,a9
"ExpireTime"=hex:99,99,99,99,99,99,99
I recommend to use the hosts entry solution and enter the name to use and the IP of the server you want to use.

Obtain client MAC address in ASP.NET Application

How can I obtain the client MAC address with C# code of someone who visits my website?
You don't have access to the MAC, you don't need it. The MAC is only known to the router who the computer is connected to.
You can't get the MAC address of the sender, because you can't get mac address behind the first router on the path to the client.
To achieve that, you have to run some code on the client machine. MAC address isn't provoided in the internet. Store some cookie with a guid on the machine, that is not as disliked as running code on the client machine. ;-)
The best thing in my oppinion is to establish a user management on your website and give the peaople a chance to register / login. Then you can track everything around the user.

How do I programmatically check to see what domain I am connected to?

If I'm connected to the local LAN here at work, I need to have my app access our server via an internal IP, otherwise, I'll need to use our external IP when out in the wild.
Currently, I just try to connect via the local IP and then try the external if it fails... but the timeout takes a bit too long and I was wondering if there's a way to find out what domain the machine is connected to before trying.
Edit: Patrick> Essentially, the app runs on a tablet pc that is connected to the local network a couple of times a day. It's roughly equal between the number of times it connects over the network and the times that it connects locally.
All machines have a domain account when they are connected to the network (and have domain accounts with a naming convention of like "LOCTabletx" where x is a number given to the machine when it's ghosted.
What I'm looking for is a fast way to see if the machine is connected on our local network or the internet. Using Environment.UserDomainName gets me LOCTabletx and not the domain name.
EDIT
If it helps anyone, I just try to DNS Resolve the name of a machine that I can guarantee will be on the network (one of the servers). It works sufficiently well for me.
Have you tried:
Environment.UserDomainName
You could also take a look at the active IP addresses on the machine, and query for one that works on your local network...
var x = NetworkInterface.GetAllNetworkInterfaces()
.Where(ni => ni.OperationalStatus == OperationalStatus.Up)
.SelectMany(ni => ni.GetIPProperties().UnicastAddresses);
// do something with the collection here to determine if you're on the right network.
// just looping & printing here for example.
foreach (var item in x)
{
Console.WriteLine(item.Address);
}
And after you've figured out the network that you're on, you could subscribe to the System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged event to handle your computer jumping networks while your app is running.
System.Environment.UserDomainName
System.Security.Principal.WindowsIdentity.GetCurrent().AuthenticationType
This will return "NTLM" when not connected to the network.
You want to look at the Network Location Awareness API. Available on Windows Vista or later, it allows you to programmatically discover which network you're connected to, and be notified when this changes.
It might be familiar to you in the form of the "Is this a home / work / public network?" dialog box.
Another way, but I don't know if it is actually any better than the other solutions is:
System.Security.Principal.WindowsIdentity.GetCurrent().AuthenticationType
this is a string that returns "Kerberos" under active directory. Not sure what it would say when not connected to the domain though.
Environment.UserDomainName ...gives you the machine name if you are not joined to a domain. It gives you the domain name if you are joined to a domain. If you take a machine that is joined to a domain off the network and out "into the wild", Environment.UserDomainName will continue to provide the domain name even if you reboot and log back in (to your domain account). Your machine caches the domain credentials for about 30 days.
If you log in to your machine account, then you will get the machine name.

Computer A can reach network web service, while computer B can't. Any ideas?

You'll have to pardon me as I wasn't sure where to place this. This overlaps in the areas of C# Web Services and Network/Security management I suppose.
I have a server that runs various web applications. One of them is a web service. The account everything runs under is a local account on the machine. It is not a domain account. I don't know why this was done how it was, but it's one of those things that's just the way is because it's the way it was done.
Super, huh?
Okay so my domain account name is given full permissions on the web serivce. Now, while the account the web service runs under is not a domain account, the machine is on our domain and in our network. So, from my computer, I can fire up a browser and type in the url to the web service in this fashion:
http://serverhere:porthere/servicename
Now the fun part. From a different machine on the same network and logged in my domain account, I cannot load the web service.
Here is the really, really nutty part. From both machines A and B, I cannot ping the server hosting the web service. Might be because pinging is turned off.
So the million dollar question is, does any one have any idea's at all as to why computer B cannot access the web service while computer A (my machine) can?
The server hosting the web service is running windows 2000. My machine is running windows xp. The machine that cannot load the web service is running Windows 2003 SP 2.
Check to make sure the subnet masks for both machines are the same; if one subnet is more restrictive than the other, it'll basically ignore messages from that machine.
There are too many options for this. And I would not bet on account permission issue. You should check the communication with network sniffer, like wireshark or network monitor. Check proxy settings. and post more information about the error you receive.
Silly suggestion, but did you check the firewall settings on the 2003 machine?

Categories