2
votes

I am trying to create and Sign-In a User using Asp.Net Identity within a SignalR Hub like so:

public string CreateUser(string username, string password)
{
    var userStore = new UserStore<IdentityUser>();
    var manager = new UserManager<IdentityUser>(userStore);

    var user = new IdentityUser() { UserName = username };
    IdentityResult result = manager.Create(user, password);

    if (result.Succeeded)
    {
        var authenticationManager = Context.Request.GetHttpContext().GetOwinContext().Authentication;
        var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
        authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
        return "Success";
    }
    else
    {
        var authenticationManager = Context.Request.GetHttpContext().GetOwinContext().Authentication;
        return "Failed: "+result.Errors;
    }
}

But GetHttpContext() is always null, could someone kindly explain why?

What is the best way to call the SignIn method from within a Hub?

1

1 Answers

1
votes

Ancient question here, but a hub is an open channel of communication, so the process of using forms auth and identity like this won't work. There is no response to set headers on or ways to set cookies. You need a regular HTTP exchange for that.