button在 本地iis上失效

这是前台的代码

  </div>
        <div>
        <br/><br/><br />
    </div>
        <div id="btn">
            <asp:Button ID="Button1" runat="server"  OnClick="Button1_Click" Text="C灯"    />
            <asp:Button ID="Button2" runat="server"  OnClick="Button2_Click" Text="N灯"   />
            <asp:Button ID="Button3" runat="server"  Text="S灯" OnClick="Button3_Click"  />
        </div>
        <div>
            <br/><br/><br /><br />
        </div>

这是后台代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    [DllImport("user32.dll")]
    static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

    [DllImport("user32.dll",
 CharSet = CharSet.Auto,
 ExactSpelling = true,
 CallingConvention = CallingConvention.Winapi)]
    public static extern short GetKeyState(int keyCode);
    bool ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;

    public static void press(byte i)
    {
        keybd_event(i, 0, 0, 0);
        keybd_event(i, 0, 2, 0);

    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        press(20);
        if (Console.CapsLock)
        {
            TextBox1.Text = "C灯开启";
        }
        else
        {
            TextBox1.Text = "C灯关闭";
        }

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        press(145);
        if (ScrollLock == false)
        {
            TextBox3.Text = "S灯开启";
        }
        else
        {
            TextBox3.Text = "S灯关闭";
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        press(144);
        if (Console.NumberLock)
        {
            TextBox2.Text = "N灯开启";
        }
        else
        {
            TextBox2.Text = "N灯关闭";
        }
    }


}


在vs中使用iis experss调试时button是正常执行的,但是换成本地iis或者发布到iis上就失效了,页面可以打开但是并不执行。

端口号对么?要不关闭防火墙试试看

权限不一样,iis experss以administrator身份运行的,iis网站一般以匿名用户(guest)身份运行,你去配置iis网站以管理员身份运行,或者user32.dll加上guest组用户读取运行权限