System.AccessViolationException: error: unknown, 86 when using WNetAddConnection2

I am trying to connect to a network resource from an internal website with alternate credentials.

[DllImport("Mpr.dll")]
private static extern int WNetAddConnection2(
    NETRESOURCE lpNetResource,
    string lpPassword,
    string lpUserID,
    int dwFlags
);

private void UseConnection(string username, string password)
{
    var resource = new NETRESOURCE();
    resource.dwType = RESOURCETYPE_DISK;
    resource.lpRemoteName = this._uncPath;
    Internals.Logging.Log.Info(string.Format("Connecting to resource '{0}'...", resource.lpRemoteName));
    int ret = -1;
    ret = WNetAddConnection2(resource, password, username, 0);
    if (ret != NO_ERROR)
    {
        WNetCancelConnection2(this._uncPath, CONNECT_UPDATE_PROFILE, true);
        throw new AccessViolationException(GetErrorForNumber(ret));
    }
}

This is an internal asp.net website that tries to connect to the SAMBA Linux share to create a file.

When I try to call UseConnection (), I get an error

System.AccessViolationException: Error: Unknown, 86

I am having difficulty starting up the meaning of this error or its causes.

The website uses Windows / NTLM authentication (without basic or anonymous access).

+4
source share

All Articles