Accessing IIS site information in Silverlight - c#

I need to get the site name and port number of the IIS sites in the Silverlight Application. How to get that?

You should be able to get this info from Application.Current.Host.Source
string server = Application.Current.Host.Source.Host;
int port = Application.Current.Host.Source.Port;

Related

Get IIS default installation directory

From what I can tell all ASP.NET web applications are placed into C:\inetpub folder by default. But is there any way to retrieve this folder in a C# code? Or do I just hard code it?
PS. I need this as a default folder for my C# program that installs my web application.
The following is limited to my experience so . . . YMMV
I do not believe that there is a true default location.
IIS will host a site in any folder that IIS has access permissions for. To determine what folder that might be, you can query the existing site list.
BUT
All is not lost. You can figure out where those sites are!
In a new IIS installation you can locate the directory of the "Default Site" created during the IIS installation.
Just keep in mind that there are no guarentees that the site will remain there. Personally, I delete the Default Site as soon as I start to configure IIS.
In IIS 7+
Site locations can be found in an XML file under IIS's configuration folder.
%systemroot%\System32\inetsrv\config\
In the XML file applicationHost.config see
The XML Node path is:
/configuration/system.applicationHost/sites/
From there you can iterate through each child node to see the various folder locations. For example, the default site will probably be listed as:
child node: site name="Default Web Site" id="1"
The .Net Way
Another trick would be to query IIS directly via .Net.
Here is the API documentation
Here are some methods to keep in your back pocket:
private static Site GetSite(string siteName)
{
Site site = (from s in (new ServerManager().Sites) where s.Name == siteName select s).FirstOrDefault();
if (site == null)
throw new Exception("The Web Site Name \"" + siteName + "\" could not be found in IIS!");
return site;
}
private static ApplicationCollection GetApplications(Site site)
{
//HttpContext.Current.Trace.Warn("Site ID3: " + site + "/n");
ApplicationCollection appColl = site.Applications;
return appColl;
}
private static String GetHostName(Site site)
{
BindingCollection bindings = site.Bindings;
String bind = null;
foreach (Binding binding in bindings)
if (binding.Host != null)
return binding.ToString();
return bind;
}
Update!
While I can't guarantee that this location will remain after install, you can also query the registry:
Check out HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\InetStp\pathWWWRoot\
I'm posting this as a seperate answer as its a completely different solution:
Do not worry where other apps are installed!
Use Web Deploy (and if you're getting creative with Web Publish, you can even set specific folder permissions)
If you need to do this pragmatically/have no access to those nifty tools...
Choose a location and set permissions on the folder to ensure that IIS is able to access it.
Then using the IIS API's register your new site.
Oh and you'll need to check those app pool permissions too

How get client (user) machine name (computer name) with jQuery or JavaScript or server-side codes. (scrutiny of possible ways)

I was working on Retrieving IP address and client machine name for few days on the internet (not on the local network)
so I found some ways for getting IP-address , but I couldn't find a way for getting client machine name or gathering some info about my web site users ?
I know there are many duplicate threads about this issue out there , but many of them are old or do n't work.
the below server side codes return SERVER NAME Not client computer name !
string UserHost_ComputerName4 = Dns.GetHostName();//Server Name
string UserHost_ComputerName5 = Environment.MachineName;//Server Name
and the below line have error on user-side , but it works on server-side page running :
string UserHost_ComputerName3 = Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"]).HostName.ToString();//Has Error
Would you lead me to a possible and workable ways (JavaScript or jQuery or server side) for getting client (users who are visiting my web site) machine name?
Try this:
string PCName = Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"]).HostName;
But if you want Name of Host PC
string HostPCName = Dns.GetHostName();
As far as I remember, in order to get:
Request.ServerVariables["REMOTE_ADDR"]
Request.ServerVariables["REMOTE_HOST"]
Request.ServerVariables["REMOTE_USER"]
you have to enable Reverse DNS Lookup for you IIS.
p.s.
atm I'm not able to test it myself, so I'm not quite sure if this will solve ur problem
Not entirely sure on all the ways you can do this, but it would be possible through ActiveX, which could be a problem as that only works in some browsers. I don't believe there is a way you can do it with pure javascript, and it's not possible serverside as the server does not get access to such details of it's connected clients.

Asp.net web app shows me as the logged in user on someone elses pc

OK, I really don't know what to search for this, so hopefully someone can help me.
I've just published an asp.net web application, and after much messing about finally got it to load in my browser.
The issue however is that when I got my mate who is sitting next to me to open the site in his browser, the page showed me as the logged in user.
In the default.aspx I run some code that grabs the users windows identity
LDAPDetails LDAPUser = new LDAPDetails(WindowsIdentity.GetCurrent().Token);
LDAPDetails then does this.
_UserName = windowsId.Name.Substring(windowsId.Name.IndexOf('\\') + 1);
_DomainName = windowsId.Name.ToString().Remove(windowsId.Name.IndexOf('\\'));
//Get users information from active directory
DirectorySearcher search = new DirectorySearcher("LDAP://DCHS");
search.Filter = String.Format("(SAMAccountName={0})", _UserName);
SearchResult result = search.FindOne();
if (result != null)
{
_FullName = result.Properties["givenName"][0].ToString() + ' ' + result.Properties["sn"][0].ToString();
_Email = result.Properties["mail"][0].ToString();
_FirstName = result.Properties["givenName"][0].ToString();
_SSID = windowsId.User.ToString();
_errMsg = "";
}
These values are then put into session variables.
It seems that WindowsIdentity.GetCurrent().Token is still using my details even though its on a machine that i'm not logged into.
Thoughts?
Agree with Tim - WindowsIdentity.GetCurrent() will be executing on server and will return the windows user under which the server process (worker process in IIS) is running which happens to be your account.
What you need to do is to set up windows/integrated authentication in both IIS and ASP.NET (web.config) and then use HttpContext.Current.User.Identity instead of WindowsIdentity.GetCurrent().
The Web application is running on a web server that you are logged into (probably your machine), so the code WindowsIdentity.GetCurrent().Token is executed on the web server, and of course that will return your credentials.
There are a bunch of different mechanisms for Web app authentication, you need to do some googling.
You are likely getting the identity of the process running on the server. If you are using IIS, this is the account setup in the application pool. Follow the instructions at this MSDN link http://msdn.microsoft.com/en-us/library/aa292114(v=vs.71).aspx for configuring Integrated Windows Authentication on IIS.
Have you tried using User.Identity.Name to get the user name?
It uses the IIdentiy interface

WebApplication at "site url" coul not be found

i have made window application, which checks new announcements on sharepoint 2007 site, if any new announcemnt is found it shows it as link in my win application.
application is running fine on my server machine, but when i try to run my window application from normal machine it gives me:
System.IO.FileNotFoundException: The
Web application at
http://Server-Name:Port-Num/lists/announcements/Allitems.aspx could not be found. Verify that you
have typed the URL correctly. If the
URL should be serving existing
content, the system administrator may
need to add a new request URL mapping
to the intended application.
this machine exists on LAN, as i can access site from my browser, but when it comes to win application it gives me the above error on following line:
string rawurl = "http://192.168.1.105:1625/";
SPSite site = new SPSite(rawurl); // this line gives error
It should throw an exception because the Server Object Model you are using like SPSite, SPWeb will not work unless it's on the server either web application or windows application.
If you want to get data from a remote sharepoint server you should use SharePoint Web Services.
This article will help you http://www.infoq.com/articles/swanson-moss-web-services
References:
http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010general/thread/570a4734-d11e-4385-bb97-8625013ebe99/
http://objectmix.com/sharepoint/296959-object-model-new-web-site-creation-remote-sharepoint-server.html
make sure the alternate access mapping is correctly set
so if you can call a page with multiple name (machine name and alias like xxxx73 and portauat) note that may only one works in visual studio so try to fix mapping in central admin or call the page with default main url.
for me noting this small tip, fix my problem in my Sharepoint 2010 UAT environment

Get the tcp port web site is served on

I need to get a tcp port of the specified web site on IIS 7 and IIS 6 using C#. I have a console application that knows the web site's name. It should find a port this web site is served on.
you can get with servervariables
Request.ServerVariables["SERVER_PORT"]
I think I can use System.DirectoryServices for IIS 6 and Microsoft.Web.Administration for IIS 7.
OK. I'm going to give you a different answer since you commented that my last answer was not the answer to your question.
Try adding a global.asax file to your asp.net application. It will have functions to handle different events on the server. For the Application_Start function, you can have some code to save the port number that the web site is running on in a file or database somewhere.
Then, the console application can access the same file or database to find the port number.
If that doesn't suit you, then perhaps a better question to ask on SO would be "How can I programmatically read the IIS settings for a web site at run time?"
By default IIS binds to port 80 (default http port) but I am sure the answer is not that simple.
Maybe you could have used the admin scripts in IIS 6.0, to iterate through the IIS objects to find the port number, but this assumes the script is physically running on the server.
The only other option is run scan of each 65535 port to see if there a html listener using wget maybe.
FOR IIS 7 ;-)
private bool checkPortIsOpen(string portNumer)
{
ServerManager serverMgr = new ServerManager();
int index = 0;
bool isOpen = true;
foreach (Site mySite in serverMgr.Sites)
{
foreach (Microsoft.Web.Administration.ConfigurationElement binding in mySite.GetCollection("bindings"))
{
string protocol = (string)binding["protocol"];
string bindingInfo = (string)binding["bindingInformation"];
if (protocol.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
string[] parts = bindingInfo.Split(':');
if (parts.Length == 3)
{
string port = parts[1];
if(port.Equals(portNumer.ToString()))
{
isOpen = false;
webSite_portInUse = mySite.Name;
}
}
}
index++;
}
}
return isOpen;
}
Had to figure this out myself today, and got the answer I wanted, so figured I'd post it into this old thread.
You can determine the port by reading the IIS Metabase, which in IIS6 and above is an xml document.
In IIS6 get the file systemroot\system32\inetserv\metabase.xml and look at the node
/configuration/MBProperty/IIsWebServer[#ServerComment=$websitename]/serverBindings
In IIS7 get the file systemroot\system32\inetserv\config\applicationHost.config
(it is xml, despite the .config extension) and look at the node
/configuration/system.applicationHost/sites/site[#name='$websitename']

Categories