在IP地址中传递凭据

时间:2017-07-19 07:51:00

标签: c# .net winforms authentication axwindowsmediaplayer

我正在尝试直接从本地网络上的网络共享访问媒体。 问题是我需要使用url传递Windows凭据。 我尝试使用登录类型9进行模拟,我尝试在URL中传递凭据,如下所示:

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

我正在尝试访问winform项目中的Windows媒体播放器中的媒体,并且播放器只是加载某些内容并进入就绪状态。 在资源管理器中键入地址时,它会要求提供凭据,这是我的预期,但在我的情况下我该怎么做呢?我觉得我已经尝试了一切......

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";

修改

由于某些原因,如果我使用machinename / servername作为媒体播放器网址中的地址,它会起作用。如果客户端只知道服务器的ip而不是名称,那么效率就不高。

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";

关于如何解决这个问题的想法?

2 个答案:

答案 0 :(得分:0)

您需要使用NetworkCredential登录远程位置。

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");

答案 1 :(得分:0)

经过一些广泛的研究后,我在帖子中找到了一个非常可行的答案:How to provide user name and password when connecting to a network share

Luke Quinane制作了以下代码:

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
}

我可以这样使用:

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";
}

谢谢卢克!