简体   繁体   English

用户单击按钮时重定向到登录页面

[英]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? 是否有我应该在FormAuthentication类中使用的函数?

Unfortunately, you need to write your own custom code for this (of course, you can use ASP.NET infrastructure). 不幸的是,您需要为此编写自己的自定义代码(当然,您可以使用ASP.NET基础结构)。 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) 单击任何按钮,检查用户是否已通过身份验证,如果没有,则重定向到登录页面(使用FormsAuthentication.RedirectToLoginPage方法)

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. 从更好的使用体验,我将生成一个java脚本,它将弹出登录提示(模式对话框),如果有必要,并通过ajax调用登录,然后重新提交形成。 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. 使用诸如jquery之类的库,您可以在表单上附加必要的script-let到所有提交按钮(或标有特定类的按钮)。

You can set authentication in files by some setting in in web.config like. 您可以通过web.config中的某些设置来设置文件中的身份验证。

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");
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果未经过身份验证,则将用户重定向到登录页面 - Redirecting user to login page if not authenticated 用户单击单选按钮时触发事件 - Firing an event when a user clicks a radio button 当用户单击按钮时,如何创建一个EWF UI页面来修改数据? - How do you create an EWF UI page that modifies data when the user clicks a button? 当用户单击按钮(而不是page_loadcomplete)时如何调用javascript方法? - How to call javascript method when user clicks on button(not on page_loadcomplete)? 当用户单击asp.net中的浏览器后退按钮时,如何快速重定向页面 - How to redirect the page quickly when the user clicks the browser back button in asp.net 当用户单击Ctrl键+链接按钮时,如何在新标签页中打开页面? - How to open a page in new tab when user clicks ctrl key + link button? 当用户仍然登录时,浏览器后退按钮导航到登录页面 - Browser back button navigates to login page when user is still logged in 重定向登录按钮 - Redirecting Login in button FormsAuthentication重定向到登录页面 - FormsAuthentication redirecting to login page 授权回调端点不断重定向到用户交互登录页面 IdentityServer - Authorize callback endpoint keeps redirecting to the user interaction login page IdentityServer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM