c# - Identifying the logged in user from different browser tab in asp.net -


i have website login option in asp.net.

if open website in 2 browser tabs , logged in same user account , navigated homescreen in both tabs , logged out 1 tab , again logged in same tab,after clicked on second tab,

how can discriminate sending request first tab or second tab code behind?

if request second tab need navigate application login screen.how can this?

in home page had added logic like

if (session["userid"] == null) {    response.redirect("login.aspx"); } 

but problem when logout first tab , login in again there, , after second tab refreshed session["userid"] not null stay there.but need redirect login page .how can achieve this??

may suggest use javascript , ajax post redirection. use myself, , find reliable, satisfying need. still think there may far better ways this. job.

below code checks tab focus/focus lost, , calls csharp code on blur , focus.

    <script type="text/javascript">         //divclearmessages         $(window).on("blur focus", function (e) {             var prevtype = $(this).data("prevtype");               if (prevtype != e.type) {   //  script please come                 switch (e.type) {                     case "blur":                         // use if want check after user changes tab                     case "focus":                         $.ajax({                             type: "post",                             url: "main.aspx/checkifsessionisnull", // call here csharp method checks session , redirect user                             contenttype: "application/json; charset=utf-8",                             datatype: "json",                             success: function (retvalue) {                                 // return value from.net method                             }                         });                         break;                 }             }              $(this).data("prevtype", e.type);         })     </script> 

in code behind add method: (remember add [webmethod] attribute)

    [system.web.services.webmethod]     public static void checkifsessionisnull()     {        if (system.web.httpcontext.current.session["userid"] == null)           httpcontext.current.response.redirect("login.aspx");     } 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -