添加额外的步骤,ASP.NET MVC认证 [英] Adding extra step to ASP.NET MVC authentication

查看:103
本文介绍了添加额外的步骤,ASP.NET MVC认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC 5网站使用标准窗体身份验证运行。

I have an MVC 5 website running using standard forms authentication.

不过,我需要添加一个额外的步骤,用户的登录过程。一旦用户被认证,我们仰望他们是否拥有访问多个办事处。如果他们这样做,我们需要向他们展示办事处名单,他们必须选择其中一个。

However I need to add an extra step to the user's login process. Once the user has been authenticated we look up whether or not they have access to multiple offices. If they do we need to show them a list of offices and they must choose one.

这是一个必不可少的步骤,他们可以直到他们这样做不被视为登录。

This is a mandatory step and they cannot be considered logged on until they do it.

我们需要创建自己的身份验证或我要补充一个检查到BaseController?

Do we need to create our own authentication or should I add a check to a BaseController?

推荐答案

您可以延长执行内置的身份验证:

You can extend the implementation of the built-in authentication:

public class OfficeSelectionAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var result = base.AuthorizeCore(httpContext);

        if (result)
        {
            if (IsOfficeSelected())
            {
                return true;
            }

            httpContext.Response.RedirectToRoute("OfficeSelection Route");
            httpContext.Response.Flush();
        }

        return false;
    }

    private bool IsOfficeSelected()
    {
        //office selection check
    }
}

然后,你需要,而不是使用默认的一此过滤器:

Then you need to use this filter instead of the default one:

[OfficeSelectionAuthorize]
public class AccountController : Controller
{
    //action methods
}

这篇关于添加额外的步骤,ASP.NET MVC认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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