ASP.NET MVC身份:多重登录路径和修改控制器的默认登录路径 [英] ASP.NET MVC Identity: Multiple Login Path and modify the default Login Path in controller

查看:183
本文介绍了ASP.NET MVC身份:多重登录路径和修改控制器的默认登录路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何更改控制器默认的验证重定向路径(/帐号/登录)?
例如我有4个控制器

How can I change the default authentication redirection path (/Account/Login) for controllers ? e.g I got 4 controllers

ABC - > / ABC /登录

ABC --> /ABC/Login

BCD - > / BCD /登录

BCD --> /BCD/Login

EFG - > / EFG /登录

EFG --> /EFG/Login

首页--->帐户/登录

Home ---> Account/Login

推荐答案

下面是一个自定义属性的授权,根据克里斯·帕拉特的想法:

Here is a custom Authorize attribute, as per Chris Pratt's idea:

public class CustomAuthorize:AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        string controller = filterContext.RouteData.Values["controller"].ToString();
        filterContext.Result = new RedirectToRouteResult(new
        RouteValueDictionary(new{ controller = controller, action = "Login" }));
    }
}

能否控制器上使用这样的:

Can be used on your controller like this:

[CustomAuthorize]
public class ABCController : Controller

这将未经授权的客户端重定向到控制器它试图访问的登录操作。记得把 [使用AllowAnonymous] 你的登录操作。

This will redirect an unauthorized client to the Login action on the controller it's trying to access. Remember to put [AllowAnonymous] on your Login actions.

这篇关于ASP.NET MVC身份:多重登录路径和修改控制器的默认登录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆