1

Currently my company is using ASP MVC on an Application. But this app is slow and somehow confused (many programmers with varied ideas) and I caught this project to refactor and migrate to Angular and Web API.

My question is: This app uses MAC Address to "authenticate" the devices, is there some way to use ASP MVC to run Angular (and there I'll get the MAC Address) and then use normally the Web API?

  • are you trying to use MVC for the MAC auth. You should be able to do the same thing with web api. MVC is very similar to web api, it is basically MC without the V. – Alex Terry Aug 15 '18 at 13:01
  • But my app will run on client (Angular) so on REST Request I can't get MAC Address. I found a solution here: https://dotnetthoughts.net/how-to-use-angular4-wth-aspnet-mvc/ – Anderson Koester Aug 15 '18 at 13:55
  • Angular is static javascript files. "Using" angular with MVC just means that you are storing the compiled js files in the wwwroot and serving them from the same webserver as static content. The only integration is the entry point on the index.html that may or may not be served by MVC. To get the MAC address from the client through javascript seems like a security cocern. Are you sure you don't mean IP address? – Alex Terry Aug 15 '18 at 15:06
  • No, I need to use MAC Address, it's a definition on the core system (like I said, many programmers with varied ideas, I would use sessions). Following the tutorial (link above) I'm able to use the actual backend's scenario – Anderson Koester Aug 15 '18 at 17:29
  • 1
    https://stackoverflow.com/questions/3385/mac-addresses-in-javascript You can't possibly be getting the MAC address directly from the client through the means your explaining. The link you provided doesn't solve the fact that a client won't reveal it's MAC address through any modern browser using just javascript. – Alex Terry Aug 15 '18 at 17:56
  • MAC address (networking) or some _Message Authentication Code_? – EdSF Aug 16 '18 at 06:32
  • @ATerry, see below my Answer. – Anderson Koester Aug 16 '18 at 10:28
  • Are you aware that MAC-addresses are esily spoofed? https://www.online-tech-tips.com/computer-tips/how-to-change-mac-address/ – trailmax Aug 16 '18 at 10:31
  • It's a private network app. The devices are running without external access, same with the application. So changed MAC Address is not a problem. – Anderson Koester Aug 16 '18 at 12:05
  • Based on your code, you are not getting the MAC address from the client. You are getting the IP address and then cross referencing the IP to get the MAC from a separate source. You don't need full blow MVC for this. – Alex Terry Aug 16 '18 at 14:41
  • You absolutely right ATerry. I didn't pay attention in this code. It justs catch the user's IP and sends an ARP to get it propertly. My bad! I'm changing the way that I'm doing the project. Thanks guys! – Anderson Koester Aug 16 '18 at 19:47

1 Answers1

0

I found a solution following this tutorial to start an Angular Application from ASP MVC: https://dotnetthoughts.net/how-to-use-angular4-wth-aspnet-mvc/

Now, running Angular from an ASP MVC, when user make the first request to IIS, ASP get the request and then I'm able to get client MAC Address (https://www.codeproject.com/Questions/709517/answer.aspx):

[DllImport( "Iphlpapi.dll" )]
private static extern int SendARP( Int32 dest, Int32 host, ref Int64 mac, ref Int32 length );
[DllImport( "Ws2_32.dll" )]
private static extern Int32 inet_addr( string ip );

private string GetMACAddress() {
    try {
        string userip = Request.UserHostAddress;
        string strClientIP = Request.UserHostAddress.ToString().Trim();
        Int32 ldest = inet_addr( strClientIP );
        Int32 lhost = inet_addr( "" );
        Int64 macinfo = new Int64();
        Int32 len = 6;
        int res = SendARP( ldest, 0, ref macinfo, ref len );
        string mac_src = macinfo.ToString( "X" );
        while ( mac_src.Length < 12 ) {
            mac_src = mac_src.Insert( 0, "0" );
        }
        string mac_dest = "";
        for ( int i = 0; i < 11; i++ ) {
            if ( 0 == ( i % 2 ) ) {
                if ( i == 10 ) {
                    mac_dest = mac_dest.Insert( 0, mac_src.Substring( i, 2 ) );
                } else {
                    mac_dest = "-" + mac_dest.Insert( 0, mac_src.Substring( i, 2 ) );
                }
            }
        }
        return mac_dest;
    } catch ( Exception err ) {
        return $"ERRO: {err.Message}";
    }
}

public ActionResult Index() {
    ViewBag.MAC = this.GetMACAddress();
    return View();
}