0

在我的网站上,我有两种登录方式。第一个用于然后用户按下登录按钮,这会触发一个 jQuery UI 对话框打开。第二个是普通视图,如果用户未授权,则使用该视图,它重定向到普通登录视图。但我喜欢做的是,如果用户未获得授权,它会打开登录 jquery ui 对话框,而不是重定向到用户预期的视图。

这就是我目前打开对话框的方式,

 $(".openLoginDialog").on("click", function (e) {
    e.preventDefault();
    $("<div></div>")
      .addClass("dialog")
      .attr("id", $(this).attr("data-dialog-id"))
      .appendTo("body")
      .dialog({
        title: $(this).attr("data-dialog-title"),
        create: function (event, ui) {},
        close: function () { $(this).remove() },
        open: function (event, ui) {},
        modal: true,
        position: ['center', 130],
        minWidth: 510,
        resizable: true,
        zIndex: 20000
      })
      .load(this.href);
  });

因为对话框中的内容是 partialView,所以这样调用:

<div class="items iconlogin highligth-br"><a class="openLoginDialog" data-dialog-id="LoginDialog" data-dialog-title="Login" href="@Url.Action("LogOn", "Authentication", new { returnUrl = Request.Url.ToString() })">Login</a></div>

另外为了帮助我控制未授权,我已经覆盖了这个调用。

public override void OnAuthorization(AuthorizationContext filterContext)
    {
      filterContext.Result = new RedirectToRouteResult(new
            RouteValueDictionary(new { controller = "Authentication", action = "AccessDenied" }));
    }

最后的手段是重定向到一个只有白色背景的拒绝访问页面,加载时打开一个对话框。

4

3 回答 3

1

我有一个 MVC Intranet 解决方案,它使用与您描述的功能类似的功能,但是它使用的是 Windows 身份验证而不是表单。

基本上我在索引中有这样的东西......

var userExists = "@ViewBag.User";
// require the user to register if they are not found
if (userExists == "") {
    $.get('Home/Register/', function (data) {
        $("#Register").html(data);
        $("#Register").dialog({
            resizable: false,
            closeOnEscape: false,
            dialogClass: 'no-close',
            typeDelay: 250
        });
    });
}

然后在控制器中,我只需将“ViewBag.User”设置为用户名(如果存在)。

于 2013-02-07T15:01:48.377 回答
0

看看这个例子:

注意autoOpen: false

于 2013-02-07T14:57:17.737 回答
0

我不熟悉asp.net,所以我无法帮助实际实施。一种方法是检查请求来自哪里,如果用户没有被授权,则将他们重定向回该页面并通过对话框点击他们。

另一种方法是了解用户是否在页面加载时通过身份验证并覆盖需要身份验证的链接

$(".authRequired").on("click", function (e) {
e.preventDefault();
      //fancy logic here to load form and dialog
 });
于 2013-02-07T15:06:11.403 回答