Do I need to replace localhost in the IIS://localhost/MimeMap when reading the Mimemap - iis

I'm reading out the mime types from IIS's MimeMap using the command
_mimeTypes = new Dictionary<string, string>();
//load from iis store.
DirectoryEntry Path = new DirectoryEntry("IIS://localhost/MimeMap");
PropertyValueCollection PropValues = Path.Properties["MimeMap"];
IISOle.MimeMap MimeTypeObj;
foreach (var item in PropValues)
{
// IISOle -> Add reference to Active DS IIS Namespace provider
MimeTypeObj = (IISOle.MimeMap)item;
_mimeTypes.Add(MimeTypeObj.Extension, MimeTypeObj.MimeType);
}
Do I need replace the localhost part when I deploy it to my live server? If not, why not and what are the implications of not doing so.
Cheers

It should not be an issue to leave the host as 'localhost'.
After all, you want to get the MimeMap of the machine your app is running on, correct?
A possible complication that I can forsee is that if you are using a third party as a host. They can do anything they want with host headers and it may be possible that localhost is not available for whatever reason.
But you should simply give it a shot and adjust if necessary.

If you leave it like 'Localhost', you will have to run this script directly on the server.
If you change it to fetch the machine name directly, you can think of running this script remotely as well.

Related

File.Exists from UNC using Azure Storage/Fileshare via IIS results in false.

Problem:
trying to get an image out of azure fileshare for manipulation. I need to read the file as an Drawing.Image for manipulation. I cannot create a valid FileInfo object or Image using uncpath (which I need to do in order to use over IIS)
Current Setup:
Attach a virtual directory called Photos in IIS website pointing to UNCPath of the Azure file share (e.g. \myshare.file.core.windows.net\sharename\pathtoimages)
This works as http://example.com/photos/img.jpg so I know it is not a permissions or authentication issue.
For some reason though I cannot get a reference to File.
var imgpath = Path.Combine(Server.MapPath("~/Photos"),"img.jpg")
\\resolves as \\myshare.file.core.windows.net\sharename\pathtoimages\img.jpg
var fi = new FileInto(imgpath);
if(fi.exists) //this returns false 100% of the time
var img = System.Drawing.Image.FromFile(fi.FullName);
The problem is that the file is never found to exist, even though I cant take that path and put it in an explorer window and return the img.jpg 100% of the time.
Does anyone have any idea why this would not be working?
Do I need to be using CloudFileShare object to just get a read of a file I know is there?
It turns out the issue is that I needed to wrap my code in an impersonation of the azure file share userid since the virtual directory is not really in play at all at this point.
using (new impersonation("UserName","azure","azure pass"))
{
//my IO.File code
}
I used this guys impersonation script found here.
Can you explain why DirectoryInfo.GetFiles produces this IOException?

How to pass web proxy address to Microsoft.WindowsAzure.Storage.OperationContext.UserHeaders?

I am writing some C# code that uses the Azure Resource Manager APIs and my CloudBlobClient needs to use a web proxy. According to the documentation for OperationContext.UserHeaders property at https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.operationcontext.userheaders.aspx, UserHeaders can be used to specify a proxy. Can you please share how this should be done properly?
Edited after Gaurav Mantri's comment.
The Azure clients below allow you to specify a proxy to be used via the httpClientHandler but the CloudBlobClient does not respect the proxy information from StorageManagementClient and there doesn't seem to be a way to pass the proxy information to the CloudBlobClient. Our users may want to specify different proxies for multiple connections and it doesn't seem the current architecture will easily allow this.
//Example code that instantiates clients with proxy information inside the httpClientHandler
armCompute = new ComputeManagementClient(tokenCredentials, httpClientHandler)
armStorage = new StorageManagementClient(tokenCredentials, httpClientHandler)
armNetwork = new NetworkManagementClient(tokenCredentials, httpClientHandler)
armResource = new ResourceManagementClient(tokenCredentials, httpClientHandler)
armSubscription = new SubscriptionClient(tokenCredentials, httpClientHandler)
I believe you're understanding it incorrectly. The documentation states:
Gets or sets additional headers on the request, for example, for proxy
or logging information.
From what I understand you use this to get or set the headers for your proxy to understand and not specify proxy configuration settings.
In order to specify proxy settings, you would need to specify those in your application configuration file (web.config or app.config).

Creating more than one site in Kentico?

Am going to use Kentico to create more than one store (Site) and assign user for each store to add/modify/delete his products, i've created 2 stores the first one with domain localhost:8080 and second one is storeone.localhost:8080 as documentation said in Kentico Doc URL, i can open first site with no problem but when i tried to switch to second Site it gives me Bad Request - Invalid Hostname .. can any one help me in this?? .. also i would appreciate it if any one help me on how to extract product data using Kentico API's as documentation provide me only with updating/modifying/removing data from database and i want to know how to display it with it's attachments like images pdf that i've uploaded it.
The best approach is to use two different ports. The reason for this is IIS is by default bound to port 80. So what I'd do is leave one site at 80 and do another at say 2. Make these bindings in IIS then go to Kentico and add your second site at localhost:2 vs. :8080. There's a conflict with port numbers. Kentico and IIS are "confused" and don't know which one to serve up. The only way it will work with the same port is to start and stop sites within Kentico.
Brenden is correct - there cannot be 2 sites running on same domain. What you need to do is configure IIS bindings. What I often do is that I configure my hosts file (C:\Windows\System32\drivers\etc) and add a few more rules like:
127.0.0.1 localhost2
127.0.0.1 localhost3
And then I can use bind my Kentico sites to these domains. Don't forget to also change the domain names in Kentico -> Sites app.
As for your second question:
It depends whether you want to get only SKUInfo object or page object where the custom data (page type fields) are stored. If you just need SKUInfo you can use something like:
// gets only corresponding SKU Info object
var singleProduct = SKUInfoProvider.GetSKUInfo(1); // SKUID from COM_SKU table
if (singleProduct != null)
{
var name = singleProduct.SKUName;
var price = singleProduct.SKUPrice;
}
If you need to get the product with all custom fields you need to use the Pages API as you would with any other page. A simple example:
// gets sku with all custom properties
var tree = new TreeProvider(MembershipContext.AuthenticatedUser);
var singleProduct = tree.SelectSingleDocument(2); // DocumentID from CMS_Document table
if (singleProduct != null)
{
// work with product
}
// or for multiple products
var products = tree.SelectNodes("custom.myProductType");
foreach (var product in products)
{
// work with products/pages
}
For the purpose of retrieving pages I would highly recommend to check this documentation article which contains a lot of examples.

Microsoft Unity - How to register connectionstring as a parameter to repository constructor when it can vary by client?

I am relatively new to IoC containers so I apologize in advance for my ignorance.
My application is a asp.net 4.0 MVC app that uses the Entity Framework with a Repository layer on top of that. It is a multi tenant application so the connection string that is used varies by the logged in client.
The connection string is determined by a 'key' that gets passed in as part of the route which indicates the client. This route data is only present on the first request of the user's session.
The route looks kind of like this: http://{host}/login/dev/
where 'dev' indicates we are using the dev database.
Currently the IoC container is registering all dependencies in the global.asax Application_Start event handler and I have the 'key' hardcoded as follows:
var cnString = CommonServices.GetDBConnection("dev");
container.RegisterType<IRequestMgmtRecipientRepository, RequestMgmtRecipientRepository>(
new InjectionConstructor(cnString));
Is there a way with Unity to dynamically register the repository based on the logged in client using the route data that is supplied initially?
Note: I am not manually resolving the repositories. They are getting constructed by the container when the controllers get instantiated.
I am stumped.
Thanks!
Quick assumption, you can use the host to identify your tenant.
the following article has a slightly different approach http://www.agileatwork.com/bolt-on-multi-tenancy-in-asp-net-mvc-with-unity-and-nhibernate-part-ii-commingled-data/, its using NH, but it is usable.
based on the above this hacked code may work (not tried/complied the following, not much of a unity user, more of a windsor person :) )
Container.RegisterType<IRequestMgmtRecipientRepository, RequestMgmtRecipientRepository>(new InjectionFactory(c =>
{
//the following you can get via a static class
//HttpContext.Current.Request.Url.Host, if i remember correctly
var context = c.Resolve<HttpContextBase>();
var host = context.Request.Headers["Host"] ?? context.Request.Url.Host;
var connStr = CommonServices.GetDBConnection("dev_" + host); //assumed
return new RequestMgmtRecipientRepository(connStr);
}));
Scenario 2 (i do not think this was the case)
if the client identifies the Tenant (not the host, ie http: //host1), this suggests you would already need access to a database to access the client information? in this case the database which holds client information, will also need to have enough information to identify the tenant.
the issue with senario 2 will arise around anon uses, which tenant is being accessed.
assuming senario 2, then the InjectionFactory should still work.
hope this helps

How to make LdapContextSource pointing to UnboundID InMemoryDirectoryServer?

I have a set of old automated test cases which are based on Spring LDAP framework. They connect to an external LDAP server. I am thinking about replacing the external server with an embedded one. The UnboundID InMemoryDirectoryServer appears attractive, especially if there is a way allowing Spring LDAP based clients to connect UnboundID-based embedded server. The question is: How to do it? I am new to LDAP, please help.
There is really not much difference between the case of an external and an embedded LDAP server. When configuring the LdapContextSource, you will have to set the url of the server to something like ldap://localhost:33389/ (assuming your embedded server listens at port 33389).
Be aware that by default the UnboundID InMemoryDirectoryServer will pick a free port randomly at runtime unless you configure it to listen to a fix port. This might help you getting started:
InMemoryDirectoryServerConfig config =
new InMemoryDirectoryServerConfig("dc=example, dc=com");
// make sure that the server listens on port 33389
config.setListenerConfigs(
new InMemoryListenerConfig("myListener", null, 33389, null, null, null));
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
ds.startListening();
// import some test data from an ldif file
ds.importFromLDIF(true,"content.ldif");

Resources