在 IP 地址中传递凭据
Passing credentials in IP address
我正在尝试直接从本地网络上的网络共享访问媒体。
问题是我需要使用 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-ACCC8E7B9050170712170712_085720_39AA_ACCC8E7B9050170712_08170712_085720_0092.mkv";
编辑
出于某种原因,如果我在媒体播放器 url 中使用 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-ACCC8E7B9050170719170719_100732_8084_ACCC8E7B9050170719_10170719_100732_E5A7.mkv";
知道如何解决这个问题吗?
您需要使用 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");
经过广泛研究后,我在主题中找到了一个非常可行的答案: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-ACCC8E7B9050170719170719_100732_8084_ACCC8E7B9050170719_10170719_100732_E5A7.mkv";
}
谢谢卢克!
我正在尝试直接从本地网络上的网络共享访问媒体。 问题是我需要使用 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-ACCC8E7B9050170712170712_085720_39AA_ACCC8E7B9050170712_08170712_085720_0092.mkv";
编辑
出于某种原因,如果我在媒体播放器 url 中使用 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-ACCC8E7B9050170719170719_100732_8084_ACCC8E7B9050170719_10170719_100732_E5A7.mkv";
知道如何解决这个问题吗?
您需要使用 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");
经过广泛研究后,我在主题中找到了一个非常可行的答案: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-ACCC8E7B9050170719170719_100732_8084_ACCC8E7B9050170719_10170719_100732_E5A7.mkv";
}
谢谢卢克!