dsw*_*tik 6 model-view-controller asp.net-mvc jquery modal-dialog
我正在一个使用jquery模式对话框的网站上做各种事情,比如登录等.
然而; 我们在使用这些问题时遇到了一个小问题..我们在许多操作方法中都使用[Authorize]属性,所以如果用户没有登录并遇到他们需要的路由,那么会发生什么被授权它显示登录页面,就像它假设,但显然这是一个模态.
无论如何长话短说,有没有办法创建一个自定义授权属性,可以触发模态而不是构成登录模式的实际视图?
在这种情况下,您可以使用自定义操作过滤器属性,如果用户未获得授权,则会打开弹出窗口.
在此操作过滤器中,只检查用户是否已登录并向ViewData集合添加布尔值.
关于控制器动作的属性.
然后在母版页中添加打开弹出窗口的代码的条件呈现.
属性的代码:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class PopupAuthorizeAttribute : AuthorizeAttribute
{
private void CacheValidateHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus)
{
validationStatus = this.OnCacheAuthorization(new HttpContextWrapper(context));
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
bool isAuthorized = false;
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (this.AuthorizeCore(filterContext.HttpContext))
{
HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
cache.SetProxyMaxAge(new TimeSpan(0L));
cache.AddValidationCallback(new HttpCacheValidateHandler(this.CacheValidateHandler), null);
isAuthorized = true;
}
filterContext.Controller.ViewData["OpenAuthorizationPopup"] = !isAuthorized;
}
}
Run Code Online (Sandbox Code Playgroud)
在母版页或其他常见视图中添加条件渲染:
<% if((bool)(ViewData["OpenAuthorizationPopup"] ?? true)) { %>
...Your code to open the popup here...
<% } %>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4070 次 |
最近记录: |