0

I have this SignalR / MVC web application I'm building.

I currently have login and logout functionnality using ajax calls.

I would like to have those transit via SignalR instead. Is that possible? I'm having a hard time understanding how the auth cookie is going to be handled.

Currently my controller action login does this

    [HttpPost]
    public ActionResult Login()
    {
        var nickname = Request.Params["nickname"];
        var password = Request.Params["password"];

        // [Some database stuff...]

        FormsAuthentication.SetAuthCookie(agencyMember.Id.ToString(), true);
    }

So to give this a try, I've setup my logout function in my hub:

    public class Proto1 : Hub
    {
    [Authorize(RequireOutgoing = false)]
    public string Logout() {
        FormsAuthentication.SignOut();
        return "ok";
    }

This does'nt work, when I debug, I get a "Response is not available in this context." exception and the logout does not go through.

Am I going wrong about this?

Thanks!

1 Answer 1

2

What's your motivation for wanting to do login and logout with SignalR?

SignalR Hub method invocations do not necessarily correspond to distinct HTTP requests (they can be triggered by a WebSocket message for example). This means that it's not always possible to modify response headers or set cookies when you are inside of a Hub method.

I would suggest continuing to login and logout using Ajax calls.

2
  • The main motivation is speed, the SignalR requests are super fast versus the ajax calls. I could send back the required info as text and have the javascript build that cookie + reinit the signalr communication... (thinking out loud here) Nov 5, 2014 at 15:22
  • Logout via ajax in your MVC controller as @halter73 suggests
    – davidfowl
    Nov 9, 2014 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.