Answered by:
Mapping Azure File Storage Server from IIS Running on Azure VM

Question
-
Hi
We are mapping a Azure File storage server from IIS running on a Azure VM.
After Azure VM restarts, the mapping fails with error code 1312. We have to restart the server until it works, which takes between 1 and 4 attempts.
Restarting IIS etc is not enough to fix the error. Only, a server restart(s) work.
Mapping works fine for console applications triggered by scheduled tasks.
We are using standard WNetAddConnection2 connection code in global.asax start method for IIS mapping to drive.
Has anyone else had this problem? If yes, did you find a solution?Tuesday, June 16, 2015 4:56 PM
Answers
-
Hi Paddy Richmond,
Please confirm whether you are able to connect and upload files to your share through Powershell And Cross-platform CLI both.
Also, you may try first saving the azure storage account credentials into your Windows first and then try "net use"
**************************************************************
The following is an example command line for persisting your storage account credentials into your VM:C:\>cmdkey /add:<yourstorageaccountname>.file.core.windows.net /user:<yourstorageaccountname> /pass:<YourStorageAccountKeyWhichEndsIn==>
Note: yourstorageaccountname is not your live id but the name in the endpoint.
CmdKey will also allow you to list the credentials it stored:
C:\>cmdkey /list
Currently stored credentials:
Target: Domain:target=filedemo.file.core.windows.net
Type: Domain Password
User: filedemoOnce the credentials have been persisted, you no longer have to supply them when connecting to your share. Instead you can connect without specifying any credentials:
C:\>net use * \\filedemo.file.core.windows.net\demo1
Drive Z: is now connected to \\filedemo.file.core.windows.net\demo1.
The command completed successfully.
***************************************************************
Hope this helps you.
Girish Prajwal
- Proposed as answer by Girish Prajwal Monday, June 22, 2015 10:50 AM
- Marked as answer by Girish Prajwal Monday, June 22, 2015 2:21 PM
Sunday, June 21, 2015 11:40 AM
All replies
-
Hi,
Thank you for reaching out to us.I am currently researching to gather more information with regards to your request. I shall revert back to you with an update at the earliest. Sincerely appreciate your patience.
Regards,
Shreya
Wednesday, June 17, 2015 6:26 AM -
Hi Shreya.
Thanks for showing an interest in this issue. We will be very grateful of any help you can give us.
This is the code we are using in Global.asax for the web application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;namespace PaperDataV3Portal
{
public class Global : System.Web.HttpApplication
{[DllImport("Mpr.dll",
EntryPoint = "WNetAddConnection2",
CallingConvention = CallingConvention.Winapi)]
private static extern int WNetAddConnection2(NETRESOURCE lpNetResource,
string lpPassword,
string lpUsername,
System.UInt32 dwFlags);[DllImport("Mpr.dll",
EntryPoint = "WNetCancelConnection2",
CallingConvention = CallingConvention.Winapi)]
private static extern int WNetCancelConnection2(string lpName,
System.UInt32 dwFlags,
System.Boolean fForce);[StructLayout(LayoutKind.Sequential)]
private class NETRESOURCE
{
public int dwScope;
public ResourceType dwType;
public int dwDisplayType;
public int dwUsage;
public string lpLocalName;
public string lpRemoteName;
public string lpComment;
public string lpProvider;
};public enum ResourceType
{
RESOURCETYPE_DISK = 1,
};//method that will mount the share on a given drive letter:
public static void MountShare(string shareName,
string driveLetterAndColon,
string username,
string password)
{
if (!String.IsNullOrEmpty(driveLetterAndColon))
{
// Make sure we aren't using this driveLetter for another mapping
WNetCancelConnection2(driveLetterAndColon, 0, true);
}NETRESOURCE nr = new NETRESOURCE();
nr.dwType = ResourceType.RESOURCETYPE_DISK;
nr.lpRemoteName = shareName;
nr.lpLocalName = driveLetterAndColon;int result = WNetAddConnection2(nr, password, username, 0);
if (result != 0)
{
throw new Exception("WNetAddConnection2 failed with error " + result);
}
}void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup .
//AuthConfig.RegisterOpenAuth();
MountShare(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("driveName"),
"z:",
System.Web.Configuration.WebConfigurationManager.AppSettings.Get("driveUserName"),
System.Web.Configuration.WebConfigurationManager.AppSettings.Get("drivepw"));}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs}
protected void Application_BeginRequest(object sender, EventArgs e)
{string path = HttpContext.Current.Request.Path.ToUpper();
if (path.Contains("/SURVEY/"))
{
String surveyReference = path.Substring(path.LastIndexOf("/SURVEY/") + 8);
if (surveyReference.Length == 4)
{
HttpContext.Current.Response.Redirect("/FMS/webforms/V2WebForm.aspx?ref=" + surveyReference);
}
else
{
HttpContext.Current.Response.Redirect("/FMS/LogIn.aspx");
}
}
}
}
}Wednesday, June 17, 2015 8:05 AM -
Hi Paddy Richmond,
In azure file share we need to mount the file share using the procedures provided by MS .
I suggest you to follow the procedures provided in the below article.
Let us know the results.
Girish Prajwal
Wednesday, June 17, 2015 11:39 AM -
Hi Girish.
Thanks for the link.
Not sure if it will fix my issue but I've scheduled some time in next week to look at it.
The reason I say that I am unsure that it will fix the issue - the final part of the link uses this command to map the drive "net use * \". This command works on our current set up 100% of the time.
The mapping fails for us where we try to map from IIS after some server restarts.
Paddy.
Thursday, June 18, 2015 7:07 AM -
Hi Paddy Richmond,
Please confirm whether you are able to connect and upload files to your share through Powershell And Cross-platform CLI both.
Also, you may try first saving the azure storage account credentials into your Windows first and then try "net use"
**************************************************************
The following is an example command line for persisting your storage account credentials into your VM:C:\>cmdkey /add:<yourstorageaccountname>.file.core.windows.net /user:<yourstorageaccountname> /pass:<YourStorageAccountKeyWhichEndsIn==>
Note: yourstorageaccountname is not your live id but the name in the endpoint.
CmdKey will also allow you to list the credentials it stored:
C:\>cmdkey /list
Currently stored credentials:
Target: Domain:target=filedemo.file.core.windows.net
Type: Domain Password
User: filedemoOnce the credentials have been persisted, you no longer have to supply them when connecting to your share. Instead you can connect without specifying any credentials:
C:\>net use * \\filedemo.file.core.windows.net\demo1
Drive Z: is now connected to \\filedemo.file.core.windows.net\demo1.
The command completed successfully.
***************************************************************
Hope this helps you.
Girish Prajwal
- Proposed as answer by Girish Prajwal Monday, June 22, 2015 10:50 AM
- Marked as answer by Girish Prajwal Monday, June 22, 2015 2:21 PM
Sunday, June 21, 2015 11:40 AM -
Hi Girish.
I created a new user and added it to the IIS_USERS group.
I used Powershell and cmdkey to store the file server account credentials for this user.
I then changed IIS to use this user.
Our existing mapping code within IIS now seems work.
I restarted the server 10 times and the mapping worked in IIS after each restart.
I'm going to monitor the server over next week and check for problems.
Thanks for your help.
Best regards, Paddy.
Monday, June 22, 2015 1:32 PM