0

如果会员用户尝试访问特定文件夹并且现在被角色允许,系统将重定向到 /Account/Index 并再次要求输入登录名和密码。

我想改变这种行为,因为用户已经登录,我只想重定向到另一个 /controller/action。

我能从这里得到一些帮助吗?提前致谢。

4

1 回答 1

0

我在所有网络应用程序中都做了类似的事情。如果用户已通过身份验证,但不满足查看页面的安全要求,我会抛出 HTTP 403 异常,然后显示 403 异常的特定视图。

这是我的自定义授权属性的片段:

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) {
    if (filterContext.HttpContext.Request.IsAuthenticated) {
        //If the user is authenticated, but not authorized to view the requested page (i.e. not a member of the correct group), return an HTTP 403 exception.
        throw new HttpException(403, string.Format("The user {0} was not authorized to view the following page: {1}", filterContext.HttpContext.User.Identity.Name, filterContext.HttpContext.Request.Url));
    } else {
        base.HandleUnauthorizedRequest(filterContext);
    }
}

这是来自我的 Global.asax 的片段,我在其中实际执行了视图响应(假设ErrorController存在一个视图,然后是一个名为Error403

protected void Application_Error() {
    var exception = Server.GetLastError();
    var httpException = exception as HttpException;

    Response.Clear();
    Server.ClearError();

    var routeData = new RouteData();
    routeData.Values["controller"] = "Error";
    routeData.Values["action"] = "Error500";

    Response.StatusCode = 500;
    Response.TrySkipIisCustomErrors = true;

    if (httpException != null) {
        Response.StatusCode = httpException.GetHttpCode();
        switch (Response.StatusCode) {
            case 403:
                routeData.Values["action"] = "Error403";
                break;
            case 404:
                routeData.Values["action"] = "Error404";
                routeData.Values["message"] = httpException.Message;
                break;
            case 500:
                routeData.Values["action"] = "Error500";
                break;
        }
    }

    IController errorsController = new ErrorController();
    var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
    errorsController.Execute(rc);
}
于 2012-11-19T15:00:00.737 回答