-2

How can I use sendkey or SendKeys.SendWait in asp.net c#.

I want use it just on localhost. It works in Visual studio but not on IIS, I changed applicationPoolIdentity to adminstrator but I always get the same problem "access denied".

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • 2
    Send keys where exactly? You cannot send keys to a client, is that what you are attempting? – Camilo Terevinto Apr 17 '18 at 14:07
  • 3
    This is a XY Problem - https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem . **Why** do you want to use `SendKeys`? – mjwills Apr 17 '18 at 14:07
  • 2
    `Sendkeys` is part of System.Windows.Forms which is aimed at desktop environment. Try using Javascript to achieve what you want. – NoviceProgrammer Apr 17 '18 at 14:13
  • So... you believe it is possible for a webpage to send keys to client? For example if the visited webpage would send ALT + F4 to the client it would close the browser? – Rand Random Apr 17 '18 at 14:13
  • It is working as designed. – Cleptus Apr 17 '18 at 14:16
  • I want to use it in this method "sp_DataReceived (object sender, SerialDataReceivedEventArgs e)" to refresh my page when I get the data from the serial port. If not how can I refresh my page in behind code ? – Mohamed Nassiri Apr 17 '18 at 14:19
  • If you are using Web Forms, you can use an UpdatePanel. – Patrick Hofman Apr 17 '18 at 14:20
  • I want use it just on LOCALHOST "Just on My Computer" – Mohamed Nassiri Apr 17 '18 at 14:21
  • 1
    The application pool identity alone doesn't help because the IIS service is denied access to all desktop apis. You can enable this by checking "Allow service to interact with desktop" on the logon tab for the "World Wide Web Publishing Service". I'm not sure about the relation between the iis service process and the child process workers but that might do it. Regardless this is all very bad practice. Try using any IPC to do the action you want. If you absolutely must, write a small executable the does the action and directly exposes an IPC endpoint for the server to use. – Tamir Daniely Apr 17 '18 at 14:22
  • Since you want the page to be refreshed have a look at: https://stackoverflow.com/questions/1206507/how-do-i-refresh-the-page-in-asp-net-let-it-reload-itself-by-code – Rand Random Apr 17 '18 at 14:24
  • @TamirDaniely, thank you very much, it's what I am looking for. – Mohamed Nassiri Apr 17 '18 at 14:37
  • FYI by bad practice I mean this is a very serious security hole. It opens up many extra ways to escalate an attack vector. You should never do this on a production site. If security is a concern, you should make sure that the IIS instance doesn't host anything else except the intended app, doesn't run any 3rd party plugins which may be less secure. And generally make sure the box doesn't have any credentials \ keys that access anything secure on your lan. – Tamir Daniely May 01 '18 at 10:02

1 Answers1

4

SendKeys is a method used for desktop development, not web. It is a Windows function, and you can't access such operating system functions from the browser (think what would happen if you could!). You are restricted to what you have in the browser: HTML, Javascript and CSS.

If you want to perform changes on the loaded HTML, you probably should look into a Javascript solution.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Think what would happen if you could ? Nothing I'm in LOCALHOST ,iis and browsers are on the same OS – Mohamed Nassiri Apr 17 '18 at 14:42
  • 1
    @MohamedNassiri When building websites, it is generally not a good idea to assume the web server will be the same machine as that running the web browser. Since, if you are planning to make that assumption, why use a website at all? A local app would be simpler. – mjwills Apr 17 '18 at 21:34