0

我正在尝试在我的网站上实现一个管理员功能,该功能允许管理员通过输入用户名来模拟用户(以查看他们看到的内容)。

现在,我在 _Layout 主视图的 _LogOn 部分视图中将此设置作为 jQueryUi 对话框,因为这将在每个页面上。但是,由于一些我以前从未处理过的问题,我不确定此时该做什么。

1)我需要检查输入的用户名是否存在,如果不存在,它应该在对话框的表单中显示一条消息。

2)提交后我需要重新加载整个页面(因此显示的内容会因新用户更新而改变)。

3) 页面重新加载后,该框应再次打开。对话框表单提交后。

所以我不知道如何实现这一点,因为部分没有控制器,我也不知道如何根据提交是否来自该特定表单来重新出现 jquery 框,也不知道如何在部分不是时获取错误消息t 基于模型。然后我考虑制作表单 AJAX,但我也不知道该怎么做,因为最终必须重新加载整个页面才能更新显示。

除了默认代码之外,对 _LogOn 部分的补充:

if (SessionUtil.IsSiteAdmin)
{
    <button id="impersonate" style="position: relative; right: 20px; float: left; top: -30px;">Impersonate</button>
    <div id="impersonate-user" title="Admin User Impersonation">
        @using (Html.BeginForm())
        {
            <p>Currently Impersonating: 
                @if (SessionUtil.User.Id != SessionUtil.UserAbsolute.Id)
                {
                    <text>
                    @SessionUtil.User.FirstName @SessionUtil.User.LastName (@SessionUtil.User.NetId)
                    </text>
                }
                else
                {
                    <text>None</text>
                }
            </p>
            <p>@Html.ActionLink("Stop Impersonating", "StopImpersonating", "Shared", new { area = "", @class = "ui-button ui-widget ui-state-default ui-corner-all" })</p>
            <div>
                <div id="ValidationSummary">
                @Html.ValidationSummary(true)
                </div>
                <fieldset>
                    <div class="editor-label">
                        @Html.Label("Username to Impersonate")
                    </div>
                    <div class="editor-field">
                        @Html.TextBox("Username")
                    </div>

                    <p>
                        <input type="submit" value="Impersonate"/>
                    </p>
                </fieldset>
            </div>
        }
    </div>
}

JS:

 <script type="text/javascript">
        $("#impersonate-user").dialog({
            autoOpen: false,
            modal: false,
            width: 400,
            height: 505,
            closeOnEscape: true,
            resizable: false,
            draggable: true
        });
        $("#impersonate")
            .button()
            .click(function () {
                $("#impersonate-user").dialog("open");
                $('.ui-dialog :button, .ui-dialog a').blur();
            });
    </script>

有人可以告诉我该怎么做吗?谢谢。

4

2 回答 2

0

我建议使用 ajax 进行名称查找,如果找不到则显示错误消息。无需重新加载页面失败。

看起来您正在使用 jQuery,所以对于 #1 和 #2:

$.ajax({
     url: actionUrl,
     data: { username: username },
     success: function() {
         window.location = window.location // I think this will refresh the page
     },
     error: function(error) {
         alert('username not found');  // do what you have to do
     }
});

What I would do for your case is have an action that you call with the above Ajax. If the username exists, it sets some flags in session, e.g.: Session["IsImpersonating"] = true and then you could have a block in your page that checks the session, and shows the dialog again:

@if ((bool)Session["IsImpersonating"]) // null check omitted
{
     <script>
           $(function() {
                 $("#myDialog").dialog();
           });
     </script>
}

FYI, to run an action with a partial, I think you just have to do the following:

@Html.Action("MyAction", "MyController")

This will make a call back to the server, just as if you did a regular page load, and plop the HTML for whatever view it returns onto the page.

Hope this helps.

于 2012-05-10T16:48:41.347 回答
0

I would suggest implementing a custom IIdentity, that can replace the current user id when you want to impersonate someone. It would return the normal user when User.Current.Name is called, or the impersonated user when User.Current.Name is used.

This is a bit more work on the front side, but then you never have to worry about it in any of your pages.. it just works like normal. In addition, you won't have to change any of your logic in the main app.

于 2012-05-10T21:20:50.773 回答