通过Silverlight 4获取MAC地址

时间:2011-03-02 21:13:31

标签: silverlight-4.0 ip-address mac-address

我试过这样做 - http://thewayithink.co.uk/post/2010/05/04/Mac-Address-in-Silverlight-4.aspx

但是3个条件总是错误的:

if ((Application.Current.IsRunningOutOfBrowser) && 

(Application.Current.HasElevatedPermissions) && 
(AutomationFactory.IsAvailable))

我猜它是因为权限和安全性的东西.. 有什么办法我可以从客户端获得物理IP地址吗? 正如我所说,我使用silverlight 4.

2 个答案:

答案 0 :(得分:3)

Silverlight的安全模型使您无法从浏览器沙箱外部的客户端计算机访问任何内容,也无法访问分配给隔离存储的(少量)磁盘空间。客户端计算机的MAC地址将属于该类别。但是,您可以使用提升的权限运行应用程序“Out of Browser”(OOB),这是此测试要检查的内容。

第一个条件表明您必须用尽浏览器 - 所以第一个问题是“您的应用程序是否已启用OOB并且已用尽broswer?”。

如果没有,则测试将失败。

然后,如果应用程序正在运行OOB,那么它也必须以提升的权限运行。是这种情况吗?

对于AutomationFactory测试 - this post上的答案意味着当应用程序用完浏览器并且权限提升时,它将成立。

答案 1 :(得分:0)

使用来自this link的此代码示例(问题也提到 here)。

public partial class MyClient : UserControl
{
    public MyClient()
    {
        MACAddressManager macAddressManager = new MACAddressManager();
        macAddressManager.OnGetMACAddressCompleted += new EventHandler(macAddressManager_OnGetMACAddressCompleted);
        macAddressManager.BeginGetMACAddress();
    }

    void macAddressManager_OnGetMACAddressCompleted(object sender, EventArgs e)
    {
        MACAddressManager manager = (MACAddressManager) sender;
        // MAC Address  value is in manager.MACAddress
    }
  }

  public class MACAddressManager
  {
    private dynamic sWbemServices;
    private dynamic sWbemSink;

    public string MACAddress { get; private set; }
    public event EventHandler OnGetMACAddressCompleted;

    private void EndGetMACAddress(object sender, EventArgs e)
    {
        dynamic objWbemObject = sender;
        MACAddress = objWbemObject.MACAddress;
        if (OnGetMACAddressCompleted != null)
            OnGetMACAddressCompleted(this, EventArgs.Empty);
    }

    public void BeginGetMACAddress()
    {
        if ((Application.Current.IsRunningOutOfBrowser) && (Application.Current.HasElevatedPermissions) && (AutomationFactory.IsAvailable))
        {
            dynamic sWbemLocator = AutomationFactory.CreateObject("WbemScripting.SWBemLocator");
            sWbemServices = sWbemLocator.ConnectServer(".");
            sWbemServices.Security_.ImpersonationLevel = 3; //impersonate

            sWbemSink = AutomationFactory.CreateObject("WbemScripting.SWbemSink");
            sWbemSink.OnObjectReady += new EventHandler(EndGetMACAddress);

            string query = "SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=true";
            sWbemServices.ExecQueryAsync(sWbemSink, query);
        }
    }
}