简体   繁体   中英

Redirecting to login page when user clicks a button

I want my anonymous user to be able to navigate throughout the website, but if he presses any button inside the site, he should be redirected to a login page. How do I achieve this?

Are there functions that I should use in the FormAuthentication class?

Unfortunately, you need to write your own custom code for this (of course, you can use ASP.NET infrastructure). A general outline will be

  1. Configure forms authentication so that all pages are marked as unsecured (ie anonymous access is allowed)
  2. On click on any button, check if user is authenticated or not and if not then redirect to login page (using FormsAuthentication.RedirectToLoginPage method)

From better use experience, instead of doing post-back in #2, I will generate a java-script that will pop-up the login prompt (a modal dialog) if necessary and do the login via ajax call and then re-submit the form. Using library such as jquery, you can attach the necessary script-let to all submit buttons (or buttons marked with specific class) on the form.

You can set authentication in files by some setting in in web.config like.

this will allow you to navigate all the pages. Now for any event in page like button click or something else you can write a function in which you can check authentication and authorization of user by Membership provider like

using System.Security.Authentication

Public void IsValidUser()
{
   if(User.Identity.Name!=string.empty)
     Response.Redirect("~login.aspx");
   else
   {
      if(!User.Identity.IsAuthenticated)
        Response.Redirect("~login.aspx");
   }
}

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