MVC登录弹出窗口而不是redirecttologin

时间:2010-11-23 15:05:12

标签: jquery asp.net-mvc login popup

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

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

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

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

TIA

2 个答案:

答案 0 :(得分:4)

alt text

这很简单,我之前用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>

查看:

    主页

    

&lt;%= Html.Encode(ViewData [“Message”])%&gt;

    

        要了解有关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>

控制器:

[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.");
            }
        }

动作过滤器:

 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);
    }

答案 1 :(得分:1)

不知何故,您希望您的客户在点击链接时知道该链接是否受到保护。如果它受到保护,请不要打开它,而是打开一个弹出窗口。

我可以想到三个糟糕的选项,也许这些会给你一些其他想法。

  1. 以某种方式在Javascript中有一个“保存”链接列表:当用户未登录时,JS会检查链接与保存列表,并采取相应的行动。
  2. 当您未登录时,单击链接时,您首先对该页面执行jQuery请求,如果响应正常,则让用户离开,如果响应是受保护的页面,则打开弹出。
  3. 不检查任何内容,让用户进入“登录页面”,但让登录页面将用户重定向回上一页,添加一些查询字符串参数,当母版页找到查询字符串参数时,让它打开登录对话框。 (希望很清楚)。实际上你确实离开了,但你确实回到原来的页面。