VB.NET MVC5 IAuthenticationFilter OnAuthenticationChallenge filterContext.Result-如何调用特定的控制器操作?

时间:2019-01-29 16:53:37

标签: .net vb.net

我有一个自定义的IAuthenticationFilter设置,除了OnAuthenticationChallenge方法充当控制器并加载视图之外,几乎可以完美运行,而我想调用特定的控制器操作来加载视图。

这是我用于OnAuthenticationChallenge的代码:

    Public Sub OnAuthenticationChallenge(ByVal filterContext As AuthenticationChallengeContext) Implements IAuthenticationFilter.OnAuthenticationChallenge
        ' called after OnAuthentication by the framework
        If filterContext.Result Is Nothing OrElse TypeOf filterContext.Result Is HttpUnauthorizedResult Then
            Dim RouteAction As String = DirectCast(filterContext.Controller, System.Web.Mvc.Controller).RouteData.Values("Action").ToString.ToLower
            Dim ReturnUrl As String = ""
            If (Not RouteAction = "login") AndAlso (Not RouteAction = "scripts") AndAlso (Not RouteAction = "css") Then
                ReturnUrl = filterContext.HttpContext.ApplicationInstance.Context.Request.Url.ToString()
            End If
            Dim LoginModel As Models.LoginModel = New Models.LoginModel()
            LoginModel.ReturnUrl = ReturnUrl
            filterContext.Controller.ViewData.Model = LoginModel

       '*** The following loads a view.  
       '*** How can I call the LoginController Login action instead ?????
            filterContext.Result = New ViewResult With {
            .ViewName = "~/Views/Login/Login.vbhtml",
            .ViewData = New ViewDataDictionary(LoginModel)
        }
        End If
    End Sub

这是LoginController代码:

请注意,我正在使用属性路由,如果这对寻找问题的答案有所帮助。

<RoutePrefix("Login")>
<AllowAnonymous>
Public Class LoginController
    Inherits MvcControllerBaseClass

    ' I have explicitly given the action a name of 'Login' as well as given the route a name of 'Login'      
    <Route("")>
    <Route("Index", Name:="Login")>
    <ActionName("Login")>
    Function Login() As ActionResult
        Dim LoginModel As Models.LoginModel = New Models.LoginModel()
        ViewBag.ReturnUrl = LoginModel.ReturnUrl
        ViewBag.LoginModel = LoginModel
        Return View("Login", "_LoginLayout")
    End Function
End Class

最终结果是它正在加载Login视图本身,我真正想做的是调用LoginController Login操作,但我似乎找不到如何做到这一点的任何示例,而不是我目前正在做的事情。

0 个答案:

没有答案