Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authenticate SignalR .NET Client with MVC5 ASP.NET Identity Framework

I have been researching this for a while and cannot seem to figure it out.

I have a .NET SignalR Client that needs to be authenticated with ASP.NET Identity (MVC5) so that it can access SignalR hubs secured with the [Authorize] attribute.

I found the following resource (Please see Authentication options for .NET Clients / Cookie):

http://www.asp.net/signalr/overview/signalr-20/security/hub-authorization

However the code sample for retrieving the cookie doesn't work due to:

authCookie = response.Cookies[FormsAuthentication.FormsCookieName];

and as I am not using Forms Authentication this produces an error.

I am unsure how to change this for ASP.NET Identity?

Am I focused on the correct approach, ie. retrieving a cookie and then adding it to the Cookie Container? or should I be passing the Username and Password in headers to a Hub and authenticating somehow this way?

I did not realise how confusing it would be to authenticate using a .NET client with SignalR and Identity Framework.

Even though it is not my use case I am also tagging the question with Xamarin as I feel this might be an issue faced by Xamarin mobile developers using the SignalR .NET client also.

As I cannot seem to find any examples on the Internet, any assistance would be much appreciated. Thanks.

like image 632
Si Bxxx Avatar asked Nov 10 '22 06:11

Si Bxxx


1 Answers

Just change this

var authCookie = response.Cookies[FormsAuthentication.FormsCookieName];

to

var authCookie = response.Cookies[".AspNet.ApplicationCookie"];

like image 112
araxis Avatar answered Nov 14 '22 21:11

araxis