Archil Labadze
Archil Labadze

Reputation: 4325

SignalR - Cannot get username

Building chat application using SignalR, I'm new with SignalR, started with Tutorial: Getting Started with SignalR 2 and MVC 5

Everything passed fine, now I want impliment Application User Name as sender, when I use method like this:

$('#displayname').val('@Context.User.Identity.Name');

Hub gets current loged in username and displays it on page, here is my Hub Code:

[HubName("chatHub")]
public class ChatHub : Hub
{
    public void Send(string name, string message)
    {
        Clients.All.addNewMessageToPage(name, message);
    }

}

Now I want impliment username with another method, this method is described Here

Code below not working, can anyone explain why, I want to understant to know method how to use SingalR. Here is code that not works:

[HubName("chatHub")]
public class ChatHub : Hub
{
    public void Send(string name, string message)
    {
        name = Context.User.Identity.Name;
        Clients.All.addNewMessageToPage(name, message);
    }

}

Upvotes: 0

Views: 92

Answers (1)

Quentin Roger
Quentin Roger

Reputation: 6538

In your startup class did you configure auth ?

With something like this :

    public void Configuration(IAppBuilder app)
    {
        var hubConfiguration = new HubConfiguration { EnableDetailedErrors = true };
        ConfigureAuth(app);
        app.MapSignalR(hubConfiguration);
    }

The following code is autogenerated when you create a new MCV project with authentication.

    public void ConfigureAuth(IAppBuilder app)
    {

        // Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<AppUserManager>(AppUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
        app.CreatePerOwinContext<AppRoleManager>(AppRoleManager.Create);

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {

             ...
        }
}

Upvotes: 1

Related Questions