简体   繁体   中英

Show popups only once when user logged in

I am working with mvc4. In my layout page , i have a popup that shows only when user is logged in.

  @if (User.Identity.IsAuthenticated)
    { 
      <div class="pop-suggest popup_window">            
         <div class="invite-add">    
           <a id="popupBoxCloseAd" class="close-popup">X</a>        
           <h4 class="invite-tit">"Earn more points, Get more products!!!"</h4>
           <div> Invite Friends</div>
        </div>            
    </div>
    }

for close This popup I use the following jquery,

 $('.close-popup').click(function () {
    close_popup();
 });
 function close_popup() {      
    $('.popup_window').fadeOut(500);       
  }

my problem is that, after login, this popup will arrive in every page refresh. How can i change this code to popup this div shows only when user is login and after close it will not come.

In my opionion you can create cookie when user log in with some kind of flag and destroy cookie on log out. I was doing something similar in my apps and works great.

You can use ViewData:

    ActionResult Register(RegisterViewModel vmodel) 
    {  
 if (GetAuthenticationResult(vmodel.UserName, vmodel.Password))
        {
            ViewData["AuthPassed"] = true;
            //.... other code   
        } 
     //... other code 
    }

And then make your ViewData to be checked in Layout for non-null equality.

Just add a flag to the session:

@{       
   if (Session["isAlreadyDisplayed"] == null) {
      ... your code ...
   }
   else {
      Session["isAlreadyDisplayed"] = true;
   }    
}

And don't forget to remove the session variable each time user logged in.

 Session.Remove("isAlreadyDisplayed");

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