MVC登录弹出窗口而不是redirecttologin

Mik*_*ica 5 asp.net-mvc jquery login popup

当用户尝试访问受保护的网页而不是重定向到登录页面时,我想弹出一个登录框.

我想使用MVC和jquery,而不是ms ajax脚本.所有页面都使用相同的母版页,我正在使用formsauthentication.当用户点击页面上的登录链接时,我有登录弹出窗口,问题是当他们点击进入受保护页面的链接时,我会被重定向到登录页面.

我想也许我可以捕获重定向登录,但我猜这不可能,因为重定向将发生在客户端脚本运行检查时?其他感觉有点笨拙的可能性是捕获页面卸载功能或页面上的所有链接,然后检查它们.

有没有人这样做过,或者你对如何去做有什么建议吗?

TIA

Lee*_*ith 4

替代文本

这很简单,我不久前用 colorbox 构建了一个 POC:

jQuery:

 <script src="../../Scripts/jquery.colorbox.js" type="text/javascript"></script>

<link href="<%= "../../Content/colorbox.css?version=" + Guid.NewGuid() %>" rel="stylesheet" type="text/css" />

<script type="text/javascript"  >


    $('FORM#login').live("submit", function() {
    Sys.Mvc.FormContext._Application_Load();
    $.colorbox.resize(); 
    });

    $(document).ready(function() {


        $('#change_password').click(function() {

            var url = $(this).attr("Href");
            $.ajax({ url: url, cache: false, success: function(data) {
                if (data != '') {
                    $.colorbox({ html: data, scrolling:false, opacity:0.85});
                }
                else {
                    alert(url);
                    window.location = url;
                }
            }

            });

            return false;
        });




    });

</script>
Run Code Online (Sandbox Code Playgroud)

看法:

主页

<%= Html.Encode(ViewData["消息"]) %>

要了解有关 ASP.NET MVC 的更多信息,请访问http://asp.net/mvc

<div><a id="change_password" href=" <%= Url.Content("~/Account/ChangePassword") %>" title="Change Password">Change Password</a></div>
<div id="logOn_Control"></div>
Run Code Online (Sandbox Code Playgroud)

控制器:

[AjaxAuthorize]
    //[Authorize]
    [HttpPost]
    public ActionResult ChangePassword(ChangePasswordModel model)
    {
        if (ModelState.IsValid)
        {
            if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
            {
                return RedirectToAction("ChangePasswordSuccess");
            }
            else
            {
                ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
            }
        }
Run Code Online (Sandbox Code Playgroud)

动作过滤器:

 public class AjaxAuthorizeAttribute : AuthorizeAttribute   
{   
    public string View { get; set; }   

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)   
    {


        if (!filterContext.HttpContext.Request.IsAjaxRequest())   
        {

            base.HandleUnauthorizedRequest(filterContext);
            return;   


        }

        var route = filterContext.HttpContext.Request.Path;
        var viewData = new ViewDataDictionary { { "returnUrl", route } };

        filterContext.Result = new ViewResult { ViewName = View, ViewData = viewData };

    }

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext.HttpContext.User.Identity.IsAuthenticated && filterContext.HttpContext.Request.IsAjaxRequest())
        {
            filterContext.Result = new EmptyResult();
            return;
        }
        base.OnAuthorization(filterContext);
    }



}
        // If we got this far, something failed, redisplay form
        ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
        return View(model);
    }
Run Code Online (Sandbox Code Playgroud)