gpt4 book ai didi

jquery - MVC 登录弹出窗口而不是重定向登录

转载 作者:行者123 更新时间:2023-12-03 23:05:03 24 4
gpt4 key购买 nike

我希望在用户尝试访问 protected 网页时弹出登录框,而不是重定向到登录页面。

我想使用 MVC 和 jquery 但不是 ms ajax 脚本来完成此操作。所有页面都使用相同的母版页,并且我正在使用表单验证。当用户单击页面上的登录链接时,我会弹出登录弹出窗口,问题是当他们单击转到 protected 页面的链接时,我会被重定向到登录页面。

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

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

TIA

最佳答案

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>

查看:

主页

<%= 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>

Controller :

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

Action 过滤器:

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

关于jquery - MVC 登录弹出窗口而不是重定向登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4257396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com