SignalR Context.User - Null with ApplicationCookie

Context.User is null in my hub, and I'm not sure why.

Starup:

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            AuthenticationMode = AuthenticationMode.Passive,
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity =
                    SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(5),
                        regenerateIdentity:
                            (manager, user) =>
                                user.GenerateUserIdentityAsync(manager, DefaultAuthenticationTypes.ApplicationCookie))
            }
        });
        app.MapSignalR();

        ConfigureWebApi(app);

        app.UseNancy();

Hub:

public class TestHub : Hub
{
    public void Hello()
    {
        Clients.All.hello(DateTime.Now.ToString("F"));
    }
    public void CurrentUser()
    {
        var user = Context.User;
        Clients.Caller.currentUser(user.Identity);
    }
}

The CurrentUser method throws an exception because Context.User is null. I am not sure what additional information would be helpful. I thought I could get this from the current owin context, but I see no way to get this. I tried to make the IAuthorizeHubConnection attribute, but I cannot find a way to get the current owin context from the IRquest object. I can just create a new one.

+4
source share
1 answer

SignalR , , (, Web API w/OWIN HostAuthenticationFilter/Attribute). , :

app.UseCookieAuthentication(new CookieAuthenticationOptions {
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    AuthenticationMode = AuthenticationMode.Active,
    Provider = new CookieAuthenticationProvider {
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
            validateInterval: TimeSpan.FromMinutes(5),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager, DefaultAuthenticationTypes.ApplicationCookie)) }
});
+5

Source: https://habr.com/ru/post/1544258/


All Articles