Val*_*mas 5 forms-authentication partial-views asp.net-mvc-3
我有一个Ajax.ActionLink导致返回部分视图.但是,如果我的FormsAuthentication到期且用户需要再次登录,则整个登录页面将作为部分视图返回.
这导致完整登录页面出现在divI部分视图中.所以它看起来像页面上的两个网页.
我[Authorize]在我的控制器和动作上使用该属性.
如何强制将登录页面作为完整视图返回?
您可以扩展该[Authorize]属性,以便您可以覆盖该HandleUnauthorizedRequest函数以返回JsonResult到您的AJAX调用.
public class AuthorizeAjaxAttribute : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext
filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
// It was an AJAX request => no need to redirect
// to the login url, just return a JSON object
// pointing to this url so that the redirect is done
// on the client
var referrer = filterContext.HttpContext.Request.UrlReferrer;
filterContext.Result = new JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new { redirectTo = FormsAuthentication.LoginUrl +
"?ReturnUrl=" +
referrer.LocalPath.Replace("/", "%2f") }
};
}
else
base.HandleUnauthorizedRequest(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
创建一个Javascript函数来处理重定向:
<script type="text/javascript">
function replaceStatus(result) {
// if redirectTo has a value, redirect to the link
if (result.redirectTo) {
window.location.href = result.redirectTo;
}
else {
// when the AJAX succeeds refresh the mydiv section
$('#mydiv').html(result);
}
};
</script>
Run Code Online (Sandbox Code Playgroud)
然后在Ajax.ActionLink的OnSuccess选项中调用该函数
Ajax.ActionLink("Update Status", "GetStatus",
new AjaxOptions { OnSuccess="replaceStatus" })
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1445 次 |
| 最近记录: |