c# - Setting the username in the model instead of javascript code -


i managed signalr chat going, , ran problem.

<script type="text/javascript">     $(function () {         // declare proxy reference hub.         var chat = $.connection.chathub;         // create function hub can call broadcast messages.         chat.client.broadcastmessage = function (name, message) {              $('#chat-body').append('<li><strong>' + name + ' :</strong> ' + message);          };          // start connection.         $.connection.hub.start().done(function () {             $('#send-chat-msg-btn').click(function () {                  // call send method on hub.                 chat.server.send('username', $('#message-input-field').val());                  // clear text box , reset focus next comment.                 $('#message-input-field').val('').focus();              });         });     }); </script> 

the username set in javascript code, when sending server.

i don't want that, people can pretend other people changing name here.

public class chathub : hub {     databasecontext db = new databasecontext();      public void send(string name, string message)     {         // character         string username = user.identity.getusername();         character character = db.characters.where(c => c.name == username).firstordefault();          // call broadcastmessage method update clients.         clients.all.broadcastmessage(name, message);     } } 

the name sent chathub
want name set here instead of js code.
not able name database cause says user not exist in current context

how can set name here in model, instead of in js code?

thank you.

normally in mvc, if you're not in controller want use httpcontext.current.user.identity. won't work correctly hubs though - if think it, hub has ton of connections , doesn't run under particular user's http context. instead, should use user property on hubcallercontext.

note populated if hub configured use authentication - otherwise server has no way know client's user name. see here. if you're self-hosting using owin you'll need configre auth module before calling mapsignalr (see this answer).


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -