0

I am trying to access media directly from a network share on our local network. The problem is that I need to pass windows credentials with the url. I have tried impersonation with logon type 9, and I have tried passing the credentials in the url like this:

@"\\username:password@ip\share_name\path\filename.mkv"

I am trying to access the media in a windows media player in a winform project, and the player just loads something and goes to a ready state. When typing the address in explorer it asks for credentials, which is what I expected, but how do I do that in my case? I feel like I have tried everything..

token = IntPtr.Zero;
LogonUser("Username", "NAS-IP", "Password",
            9, 0, ref token);
person = new WindowsIdentity(token).Impersonate();

axWindowsMediaPlayer1.URL = @"\\ip\camera_share\axis-ACCC8E7B9050\20170712\08\20170712_085720_39AA_ACCC8E7B9050\20170712_08\20170712_085720_0092.mkv";

EDIT

For some reason it works if I use the machinename/servername as address in the media player url. This is just not that efficient if the client only knows the ip of the server not the name.

token = IntPtr.Zero;
LogonUser("username", "serverip", "password",
            9, 0, ref token);
person = new WindowsIdentity(token).Impersonate();
axWindowsMediaPlayer1.URL = @"\\servername\camera_share\axis-ACCC8E7B9050\20170719\10\20170719_100732_8084_ACCC8E7B9050\20170719_10\20170719_100732_E5A7.mkv";

Any idea on how to work around that?

Melac
  • 1
  • 1
  • 5
  • Possible duplicate of [How to provide user name and password when connecting to a network share](https://stackoverflow.com/questions/295538/how-to-provide-user-name-and-password-when-connecting-to-a-network-share) – jAC Jul 19 '17 at 07:54

2 Answers2

0

You need to use NetworkCredential to log into remote locations.

using System.IO;
using System.Net;

NetworkCredential theNetworkCredential = new 
NetworkCredential(@"domain\username", "password");
CredentialCache theNetCache = new CredentialCache();
theNetCache.Add(new Uri(@"\\computer"), "Basic", theNetworkCredential);
string[] theFolders = Directory.GetDirectories(@"\\computer\share");
Psystec
  • 1
  • 1
  • 4
0

After some extensive research I found a very viable answer in the thread: How to provide user name and password when connecting to a network share

Luke Quinane made the following code:

public class NetworkConnection : IDisposable
{
    string _networkName;

    public NetworkConnection(string networkName, 
        NetworkCredential credentials)
    {
        _networkName = networkName;

        var netResource = new NetResource()
        {
            Scope = ResourceScope.GlobalNetwork,
            ResourceType = ResourceType.Disk,
            DisplayType = ResourceDisplaytype.Share,
            RemoteName = networkName
        };

        var userName = string.IsNullOrEmpty(credentials.Domain)
            ? credentials.UserName
            : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);

        var result = WNetAddConnection2(
            netResource, 
            credentials.Password,
            userName,
            0);

        if (result != 0)
        {
            throw new Win32Exception(result, "Error connecting to remote share");
        }   
    }

    ~NetworkConnection()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        WNetCancelConnection2(_networkName, 0, true);
    }

    [DllImport("mpr.dll")]
    private static extern int WNetAddConnection2(NetResource netResource, 
        string password, string username, int flags);

    [DllImport("mpr.dll")]
    private static extern int WNetCancelConnection2(string name, int flags,
        bool force);
}

[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
    public ResourceScope Scope;
    public ResourceType ResourceType;
    public ResourceDisplaytype DisplayType;
    public int Usage;
    public string LocalName;
    public string RemoteName;
    public string Comment;
    public string Provider;
}

public enum ResourceScope : int
{
    Connected = 1,
    GlobalNetwork,
    Remembered,
    Recent,
    Context
};

public enum ResourceType : int
{
    Any = 0,
    Disk = 1,
    Print = 2,
    Reserved = 8,
}

public enum ResourceDisplaytype : int
{
    Generic = 0x0,
    Domain = 0x01,
    Server = 0x02,
    Share = 0x03,
    File = 0x04,
    Group = 0x05,
    Network = 0x06,
    Root = 0x07,
    Shareadmin = 0x08,
    Directory = 0x09,
    Tree = 0x0a,
    Ndscontainer = 0x0b
}

Which I could use like this:

using (new NetworkConnection(@"\\IP", new NetworkCredential("Username", "Password", System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName)))
{
     axWindowsMediaPlayer1.URL = @"\\IP\camera_share\axis-ACCC8E7B9050\20170719\10\20170719_100732_8084_ACCC8E7B9050\20170719_10\20170719_100732_E5A7.mkv";
}

Thanks Luke!

Melac
  • 1
  • 1
  • 5