Adding extra step to ASP.NET MVC authentication

橙三吉。 提交于 2019-12-22 11:29:19

问题


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.

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


回答1:


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
}


来源:https://stackoverflow.com/questions/29094551/adding-extra-step-to-asp-net-mvc-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!