MVC授权 - 多重登录页面 [英] MVC Authorization - multiple login pages

查看:110
本文介绍了MVC授权 - 多重登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个MVC控制器,它重定向到登录页面时未登录用户的使用下列方法。

  [授权]
公众的ActionResult搜索(){
  返回查看();
}[授权]
公众的ActionResult编辑(){
  返回查看();
}

是否有一个快速/易/标准重定向第二个动作比?

在web.config文件中定义的页面之外的不同的登录页面的方式

还是我必须做的是这样

 公众的ActionResult编辑(){
  如果(IsUserLoggedIn)
    返回查看();
  其他
     返回ReturnRedirect(/ Login2身份);
}


解决方案

我认为这是可能通过创建自定义过滤器的授权:

 公共类CustomAuthorization:AuthorizeAttribute
{
    公共字符串LoginPage {搞定;组; }    公共覆盖无效OnAuthorization(AuthorizationContext filterContext)
    {
        如果(!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            filterContext.HttpContext.Response.Redirect(LoginPage);
        }
        base.OnAuthorization(filterContext);
    }
}

在你的行动:

  [CustomAuthorization(LoginPage =〜/主页/ Login1)]
公众的ActionResult搜索()
{
  返回查看();
}[CustomAuthorization(LoginPage =〜/主页/ Login2身份)]
公众的ActionResult编辑()
{
  返回查看();
}

I have a the following methods in an MVC Controller which redirect to the login page when a user is not logged in.

[Authorize]
public ActionResult Search() {
  return View();
}

[Authorize]
public ActionResult Edit() {
  return View();
}

Is there a quick/easy/standard way to redirect the second action to a different login page other than the page defined in the web.config file?

Or do I have to do something like

public ActionResult Edit() {
  if (IsUserLoggedIn)
    return View();
  else 
     return ReturnRedirect("/Login2");
}

解决方案

I think it is possible by creating a custom authorization filter:

public class CustomAuthorization : AuthorizeAttribute
{
    public string LoginPage { get; set; }

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            filterContext.HttpContext.Response.Redirect(LoginPage);
        }
        base.OnAuthorization(filterContext);
    }
}

In your action:

[CustomAuthorization(LoginPage="~/Home/Login1")]
public ActionResult Search() 
{
  return View();
}

[CustomAuthorization(LoginPage="~/Home/Login2")]
public ActionResult Edit() 
{
  return View();
}

这篇关于MVC授权 - 多重登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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