Saber Amani
Saber Amani

Reputation: 6489

IsAuthenticated property is still false after user authentication in SignalR

I've a logged in user in my MVC web application, but IsAuthenticated property in my Hub class is still false. Here is my log in and redirection sequences:

  1. User submits a Login form.
  2. Application validates user information.
  3. Application creates FormsAuthenticationTicket and encrypts ticket using FormsAuthentication class.
  4. Application adds encrypted ticket to response cookies in FormsAuthentication.FormsCookieName path.
  5. Then I redirect it to a view that start SignalR connection, when I check is user is authenticated or not in override OnConnected method, I find that it is still false.

How should I handle this issue, I don't have any problem with authorization in SignalR 1.1, I'm using same code but this time with SignalR 2.0.

Update: Actually it's not still false in SignalR Hub class it's false in all over application domain.

Any advice will be helpful. Thanks in advance.

Upvotes: 0

Views: 1004

Answers (2)

Saber Amani
Saber Amani

Reputation: 6489

In Visual Studio 2013, default MVC template uses ASP.Net Identity authentication system and puts <authentication mode="None" /> in web.config, so this prevents application to creates HttpCookie. I just change it to <authentication mode="Forms" /> and now it's working.

Upvotes: 0

Teddy
Teddy

Reputation: 797

If you add cookie into response by yourself, you need to resolve it by yourself as well.

if(Login(model.UserName, model.Password)){
    // ecrypt user data and add to cookie
    // your code
}

// Global file code
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
    // decrypt cookie and set current principal
    // your code

    Thread.CurrentPrincipal = HttpContext.Current.User = principal;
}

In additional, you need a customer principal class to store user info if traditional GenericPrincipal does not fit.

Upvotes: 2

Related Questions