I'm developping a web application that sometimes acces a shared drive not in the Domain with a local machine logon/password.
For that I'm using some code found on the net (WNetAddConnection2A) and it's working pretty well ....
But then I moved my web application to an other server, and now I got a 1312 error (System error 1312 has occurred - - A specified logon session does not exist. It may already have been terminated.)
So basicly I have 2 Web Server in the same domain and one shared folder not in the domaine protected by a local machine account.
It's working perfectly on one of the web server and not on the other. They both have the same security configutation in IIS ...
Some code sample :
Expand|Select|Wrap|Line Numbers
- //WIN32API - WNetAddConnection2
- [DllImport("mpr.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
- private static extern int WNetAddConnection2A(
- [MarshalAs(UnmanagedType.LPArray)]
- NETRESOURCE[] lpNetResource
- ,
- [MarshalAs(UnmanagedType.LPStr)]
- string lpPassword
- ,
- [MarshalAs(UnmanagedType.LPStr)]
- string lpUserName
- ,
- int dwFlags
- );
- public static void ConnectAs(string pShare, string pDomain, string pUser, string pPwd)
- {
- int err = 0;
- try
- {
- string sUser = string.Empty;
- if (pDomain.Length < 0)
- sUser = pDomain + "\\" + pUser;
- else
- sUser = pUser;
- int dwFlags = 0;
- pShare = GetServerName(pShare);
- NETRESOURCE[] nr = new NETRESOURCE[1];
- nr[0].lpRemoteName = pShare;
- nr[0].lpLocalName = ""; //mLocalName;
- nr[0].dwType = 1; //disk
- //nr[0].dwDisplayType = 0;
- //nr[0].dwScope = 0;
- //nr[0].dwUsage = 0;
- //nr[0].lpComment = "";
- //nr[0].lpProvider = "";
- err = WNetAddConnection2A(nr, pPwd, sUser, dwFlags);
- if (err != 0)
- throw new Exception("Unable to connect to " + pShare
- + ". Error=" + err.ToString());
- }
- catch(Exception ex)
- {
- throw new Exception("Fatal error : " + ex.ToString());
- }
- }