【问题标题】:Web API, Angular and MAC AddressWeb API、Angular 和 MAC 地址
【发布时间】:2019-01-22 08:07:57
【问题描述】:

目前我的公司在应用程序上使用 ASP MVC。但是这个应用程序很慢而且有点混乱(许多程序员有不同的想法),我抓住了这个项目来重构并迁移到 Angular 和 Web API。

我的问题是:这个应用程序使用 MAC 地址来“验证”设备,有什么方法可以使用 ASP MVC 来运行 Angular(然后我会得到 MAC 地址)然后正常使用 Web API?

【问题讨论】:

  • 您是否尝试使用 MVC 进行 MAC 身份验证。你应该可以用 web api 做同样的事情。 MVC 和 web api 很像,基本上就是没有 V 的 MC。
  • 但我的应用程序将在客户端 (Angular) 上运行,因此在 REST 请求中我无法获取 MAC 地址。我在这里找到了解决方案:dotnetthoughts.net/how-to-use-angular4-wth-aspnet-mvc
  • Angular 是静态 javascript 文件。 “使用”角度与 MVC 只是意味着您将编译的 js 文件存储在 wwwroot 中,并从同一个网络服务器作为静态内容提供它们。唯一的集成是 index.html 上的入口点,它可能由 MVC 提供,也可能不提供。通过 javascript 从客户端获取 MAC 地址似乎是一个安全问题。你确定你说的不是 IP 地址吗?
  • 不,我需要使用MAC地址,它是核心系统上的定义(就像我说的,许多想法不同的程序员,我会使用会话)。按照教程(上面的链接),我可以使用实际的后端场景
  • stackoverflow.com/questions/3385/mac-addresses-in-javascript您不可能通过您解释的方式直接从客户端获取MAC地址。您提供的链接并不能解决客户端不会通过任何仅使用 javascript 的现代浏览器显示其 MAC 地址的事实。

标签: asp.net asp.net-mvc angular asp.net-web-api mac-address


【解决方案1】:

我在本教程之后找到了一个从 ASP MVC 启动 Angular 应用程序的解决方案:https://dotnetthoughts.net/how-to-use-angular4-wth-aspnet-mvc/

现在,从 ASP MVC 运行 Angular,当用户向 IIS 发出第一个请求时,ASP 获取请求,然后我能够获取客户端 MAC 地址 (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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 2023-03-09
    相关资源
    最近更新 更多