用户和编辑者之间的 SignalR 聊天 asp.net

SignalR Chat asp.net between user and editor

如何让它适用于当前用户和任何编辑器用户帐户?即使我以普通用户登录并隐身在其中一个编辑帐户上,它也显示出我在自言自语,因为它进入了 名称 != 管理员。

<script>
    $(function () {
        // Reference the auto-generated proxy for the hub.
        var chat = $.connection.chatHub;
        // Create a function that the hub can call back to display messages.
        chat.client.addNewMessageToPage = function (name, message) {
            if (name == "Admin") {
                // Add the message to the page.
                $('#discussion').append('<p style="color:green; text-align:left; width:500px"><strong><img = src="https://www.phplivesupport.com/pics/icons/avatars/public/avatar_7.png" title="Admin">'
                    + ' </strong> ' + htmlEncode(message) + '</p>');
            }
            else if (name != "Admin") {
                // Add the message to the page.
                $('#discussion').append('<p style="color:blue;text-align:right;"><strong><img = src="https://www.phplivesupport.com/pics/icons/avatars/public/avatar_71.png" title="Peter">'
                    + ' </strong> ' + htmlEncode(message) + '</p>');
            }
        };
        // Get the user name and store it to prepend to messages.
        var currentMember= '@Html.Raw(@ViewBag.Name)';
        alert(currentMember);
        $('#displayname').val(currentMember);
        //$('#displayname').val(prompt('Enter your name:', ''));
        // Set initial focus to message input box.
        $('#message').focus();
        // Start the connection.
        $.connection.hub.start().done(function () {
            $('#sendmessage').click(function () {
                // Call the Send method on the hub.
                chat.server.send($('#displayname').val(), $('#message').val());
                // Clear text box and reset focus for next comment.
                $('#message').val('').focus();
            });
        });
    });
    // This optional function html-encodes messages for display in the page.
    function htmlEncode(value) {
        var encodedValue = $('<div />').text(value).html();
        return encodedValue;
    }
</script>

原来是这样。

 <script>
        $(function () {
            // Reference the auto-generated proxy for the hub.
            var chat = $.connection.chatHub;
            // Create a function that the hub can call back to display messages.
            chat.client.addNewMessageToPage = function (name, message) {
                if (name == "true") {
                    // Add the message to the page.
                    $('#discussion').append('<p style="text-align:left; text-shadow: 2px 2px 3px red"><strong><img = src="/Images/admin_icon.png" title="Admin">'
                        + ' </strong> ' + htmlEncode(message) + '</p>');
                }
                else if (name != "true") {
                    // Add the message to the page.
                    $('#discussion').append('<p style="text-align:right;text-shadow: 2px 2px 3px teal;"><strong>'
                        + ' </strong> ' + htmlEncode(message) + '<img = src="/Images/client_icon.png" title="Client">' + '</p>');
                }
            };
            // Get the user name and store it to prepend to messages.
            var currentMember= '@(User.IsInRole("Administrator") ? "true" : "false")';
            //alert(currentMember);
            $('#displayname').val(currentMember);
            //$('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub.
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });
            });
        });
        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>