简体   繁体   English

重定向到登录页面

[英]Redirect To the log in page

我有一个ASP MVC 5网站,我有多个控制器和视图,如果未通过if(User.Identity.Is Authenticated)的所有操作,如果未通过身份验证,如何将用户芳香地重定向到“登录”视图

Decorate your controllers (if you want all actions within that controller to require authentication) or specific actions with the [Authorize] attribute. 使用[Authorize]属性装饰控制器(如果您希望该控制器内的所有操作都需要认证)或特定操作。 You can specify the login url the user gets redirected to in your web.config file. 您可以在web.config文件中指定用户重定向到的登录URL。

[Authorize]
public class UserController : Controller
{
    public ActionResult Index()
    {
        // Must be authorized
    }

    public ActionResult Users()
    {
        // Must be authorized
    }
}    

public class ProductController : Controller
{
    public ActionResult Index()
    {
        // Doesn't require authorization
    }

    [Authorize]
    public ActionResult Products()
    {
        // Must be authorized
    }
}

Further reading: 进一步阅读:

http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute(v=vs.118).aspx http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute(v=vs.118).aspx

For this task you can make use of the [Authorize] attribute, which you can implement at either the Class or Action level: 对于此任务,您可以使用[Authorize]属性,该属性可以在“类”或“操作”级别实现:

[Authorize] 
public class AccountController : Controller
{
    public AccountController () { . . . }

    public ActionResult Register() { . . . }

    public ActionResult Manage() { . . . }

    public ActionResult LogOff() { . . . }
. . .
} 

In the above instance attempting to access any action method in the controller will redirect the user to the login page, after which a further redirection to the initially requested action will be made. 在上面的实例中,尝试访问控制器中的任何操作方法都会将用户重定向到登录页面,然后将进一步重定向到最初请求的操作。

For further information on how to use the Autohorize attribute please see the MSDN article , as it has expanded examples and background information. 有关如何使用Autohorize属性的更多信息,请参见MSDN文章 ,该文章已扩展了示例和背景信息。

Answer 1 : 答案1:

You can use inbuilt [Authorize] attribute in MVC.. 您可以在MVC中使用内置的[Authorize]属性。

 [Authorize] 
 public class YourController : Controller
 {
   public ActionResult YourAction()
   {

   }
 }

the [Authorize] attribute will check whether user is authenticated or not if not then it will redirect user to login page automatically [Authorize]属性将检查用户是否通过身份验证,如果没有通过,它将自动将用户重定向到登录页面

Answer 2 : 答案2:

Or you can make your custom filter attribute as : 或者,您可以将自定义过滤器属性设置为:

 [AuthoriseUser]
 public class YourController : Controller
 {
   public ActionResult YourAction()
   {

   }
 }

public class AuthoriseUserAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
   // Check user is authenticated 

   // if user is not authenticated then do as :

     filterContext.Result = new RedirectToRouteResult(new
     RouteValueDictionary(new { controller = "Login", action = "Index" }));
}
}

Answer 3 : 答案3:

As you have said in your comments section that you don't want to write [Authorize] attribute on every controller in your project then below answer will help you : 正如您在评论部分中所说,您不想在项目中的每个控制器上编写[Authorize]属性,那么下面的答案将对您有所帮助:

public class YourController : Controller
{
  protected override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    //....Check user authentication here           
  }
}

public class MyController : YourController 
{
  public ActionResult Myaction()
  {
    // ...
  }
}

In above answer all you have to do is to make your custom base controller YourController and then you can put your authentication stuff there and then every controller in your web app will inherit YourController instead of inbuilt base Controller class . 在上面的答案中,您要做的就是创建自定义的基础控制器YourController ,然后可以在其中放置身份验证内容,然后Web应用程序中的每个控制器都将继承YourController而不是内置的基础Controller class

You can use the events in the global.asax to authenticate and authorize on every request. 您可以使用global.asax中的事件对每个请求进行身份验证和授权。

Application_AuthenticateRequest : Fired when the security module has established the current user's identity as valid. Application_AuthenticateRequest :当安全模块已将当前用户的身份确定为有效时触发。 At this point, the user's credentials have been validated. 至此,用户的凭证已经通过验证。

Application_AuthorizeRequest : Fired when the security module has verified that a user can access resources. Application_AuthorizeRequest :在安全模块已验证用户可以访问资源时触发。

So, something like this (this isn't complete though) 因此,这样的事情(虽然还不完整)

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
            if (User.Identity.IsAuthenticated)
            {

                // you can re-direct them for example
            }       
    }

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

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