[英]trigger an jQuery UI Dialog if user not authorized
在我的網站上,我有兩種登錄方式。 首先使用它,然后用戶按下“登錄”按鈕,這將觸發jQuery UI對話框打開。 第二個是普通視圖,如果未授權用戶使用該視圖,該視圖將重定向到普通登錄視圖。 但是我喜歡做的是,如果用戶未被授權,它將打開Login 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" }));
}
不得已的方法是重定向到訪問被拒絕的頁面,只有白色背景,該頁面在加載時打開,並顯示一個對話框。
我有一個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”設置為用戶名(如果存在)。
我對asp.net不熟悉,所以我將無法為實際實施提供幫助。 一種方法是檢查請求來自何處,以及是否未授權用戶將其重定向回該頁面,然后使用對話框單擊它們。
另一種方法是知道用戶是否在頁面加載時通過了身份驗證,並覆蓋了需要身份驗證的鏈接
$(".authRequired").on("click", function (e) {
e.preventDefault();
//fancy logic here to load form and dialog
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.