-1

Guys I am getting error in the following code in trying to get mac address of client machine in asp.net c#.When I run same code on local machine it works perfectly but when I upload same code to server I get the error as shown below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Security.Policy;
using System.Management;
using System.Management.Instrumentation;
public partial class GetMac : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string id = "";
        ManagementObjectSearcher query = null;
        ManagementObjectCollection queryCollection = null;

        try
        {
            query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
            queryCollection = query.Get();
            foreach (ManagementObject mo in queryCollection)
            {
                if (mo["MacAddress"] != null)
                {
                    id = mo["MacAddress"].ToString();
                    Response.Write(id+"<br/>");

                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Source);
            Response.Write(ex.Message);
        }
    }
}

Error is like this:

App_Web_klgxzt4kAttempt by security transparent method 'GetMac.Page_Load(System.Object, System.EventArgs)' to access security critical method 'System.Management.ManagementObjectSearcher..ctor(System.String)' failed. Assembly 'App_Web_klgxzt4k, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is partially trusted, which causes the CLR to make it entirely security transparent regardless of any transparency annotations in the assembly itself. In order to access security critical code, this assembly must be fully trusted.
user3807218
  • 47
  • 1
  • 6

2 Answers2

1

As Henk said in your code you are getting server MAC address. On your local machine it works only because your local machine is client AND server and you run your code with higher priviledge. You can get client MAC address using some client side script. Here you can find a discution about it 'MAC addresses in JavaScript'

Community
  • 1
  • 1
pen2
  • 759
  • 5
  • 16
-2

Got it. For this we need to give trust level to our application in web.config For that reason we need to add following code in system.web in web.config file.

<trust level="Full" originUrl=""/>

It really helps thx all.

user3807218
  • 47
  • 1
  • 6
  • 1
    There might be a small chance that you didn't get it so I will repeat it: You will not get client MAC address using your code even if you set proper priviledges by setting trust level. The code that you pasted will give you only the server MAC address. – pen2 Aug 11 '14 at 12:51
  • u dont seem to be professsional and helpful...dont need your useless advice. – user3807218 Aug 12 '14 at 07:24
  • You cannot get the client's MAC address from ASP.NET. If you have some way to run code on the client (e.g. with Silverlight) then it should be able to get it. – Kevin Panko Aug 12 '14 at 15:12