我需要show popup对话框来授权客户端,我看到了这个解决方案authorize attribute to launch a modal。但是这个解决方案用模态对话框显示私有视图,如果我点击模型对话框的关闭按钮,我仍然保持私人视图。这不好,不是吗?我也想。
目标是:当我点击私人视图的链接时,我们检查授权。如果客户端未经授权,我们会显示弹出对话框如果客户输入了正确的登录/通过,我们会重定向到私人页面。
我怎么能这样做?
PersonalController
public class PersonalController : BaseController
{
[PopupAuthorize]
public ActionResult Index()
{
return View(BaseViewObject);
}
[PopupAuthorize]
public ActionResult PersonalPrice()
{
return View(BaseViewObject);
}
}
PopupAuthorizeAttribute
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public class PopupAuthorizeAttribute : AuthorizeAttribute
{
private void CacheValidateHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus)
{
validationStatus = OnCacheAuthorization(new HttpContextWrapper(context));
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
base.HandleUnauthorizedRequest(filterContext);
}
protected override HttpValidationStatus OnCacheAuthorization(HttpContextBase httpContext)
{
return base.OnCacheAuthorization(httpContext);
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
Debug.WriteLine("OnAuthorization");
bool isAuthorized = false;
if (filterContext == null) {
throw new ArgumentNullException("filterContext");
}
if (AuthorizeCore(filterContext.HttpContext)) {
HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
cache.SetProxyMaxAge(new TimeSpan(0L));
cache.AddValidationCallback(CacheValidateHandler, null);
isAuthorized = true;
}
filterContext.Controller.ViewData["OpenAuthorizationPopup"] = isAuthorized == false;
}
}
_layout
<!-- other html code -->
@if (User.Identity.IsAuthenticated == false && ((bool)(ViewData["OpenAuthorizationPopup"] ?? false))) {
@Html.Partial("_LogInPartial", Model.Auth)
}