简体   繁体   中英

logout user in onbeforeunload and send log to server

I'm running a silverlight web app and I want to log the user out if he closes the browser, change site etc. in case they forget to click the logout button and I want to do this in the web page that hosts the silverlight.

I tried following a tutorial: adding webservice.htc and calling the web service using javascript which looks like this

html body tag:

<body id="service" style="behavior:url(webservice.htc)">

script:

window.onbeforeunload = function(){
   // set web service
   service.useService(
   "https://myhost/myService/service.svc?wsdl",
   "myService");

   // call web service method
   service.myService.callService("LogInUser", -1);
}

the onbeforeunload does get called if i just try to show a message like

return "onbeforeunload called!";

EDIT: My web service is in SOAP and would like to consume in using javascript or or another option in my ASP.net 3.5 hosting the silverlight and is supposed to be viewed in IE8+. Any options that could complete this?

I'm not familiar with the method you show, but I assume the document simply gets unloaded before anything can happen in your web service.

The only cross-browser way to do this that I know of is to make a synchronous Ajax call. More here , or in jQuery use the async: false flag.

I'm not sure whether this is a good idea at all though. For example, a user might have the same session open in a different browser window. With your method, closing one of them will log them out of both of them. Also, a synchronous Ajax request can freeze the browser if the network goes down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM