这有点难以解释。基本上我有一个共享视图的MVC应用程序。共享视图是标题,然后我根据用户选择的选项卡加载视图。
我的问题如下:
随附的视图有一个网格,用户可以点击该网格并从中获取详细信息。详细信息页面是局部视图。我正在尝试应用MVC安全过滤器,并在会话到期时重定向到登录页面。我有一个自定义类覆盖HandleUnauthorizedRequest。
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using PortalAPI.SPModels;
using SICommon.Enums;
using SICommon.LoggingOperations;
namespace Portal.Security {
public class AuthorizedUser : AuthorizeAttribute {
public bool IsAuthorized { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext) {
var sessionHash = Convert.FromBase64String(httpContext.Request.Cookies.Get("hash")?.Value);
if (sessionHash == null)
return this.IsAuthorized = false;
if (TransmitToSP.TransmitAPIRequest<APIRequest, SICommon.CommonModels.APIResponse>(
new APIRequest() {
UserRole = (UserRole)((CustomPrincipal)httpContext.User)?.CustomIdentity?.UserRoles[0],
SessionHash = sessionHash,
RequestStartTime = DateTime.UtcNow,
CallerMethod = "AuthorizeCore"
}, "ValidateSession").ErrorCode == 24) {
if (httpContext.Request.Cookies.AllKeys.Contains("hash")) {
FormsAuthentication.SignOut();
HttpContext.Current.Request.Cookies.Remove("hash");
return this.IsAuthorized = false;
}
}
return this.IsAuthorized = true;
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) {
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary(
new {
controller = "Account",
action = "Login"
}
)
);
base.HandleUnauthorizedRequest(filterContext);
}
}
}
我的主要课程代码:
namespace Portal.Controllers {
[AuthorizedUser(IsAuthorized = true)]
public class RequestsController : Controller {
public PartialViewResult GetDetails(){
return PartialView("_DetailsPartial",
new DetailsModel {
RequestToDisplay = requestToDisplay
}
);
}
}
}
重定向在部分视图下加载。尝试解决此问题,使其仅在主视图上加载。
我注意到视图已嵌入主视图中。重定向将插入页眉和页脚之间。