I am trying to use my own principle in my SignalR Hub OnConnected method. I tried the following:
- Context.Request.GetHttpContext (). User
- HttpContext.Current.User
- Thread.CurrentPrincipal
but no luck ..
He continues to throw an error:
Unable to cast object of type 'System.Web.Security.RolePrincipal' to type 'MVCSample.Biz.Profile.MyCustomPrincipal'.
Is there access to custom principles in SignalR hubs?
Thank!
This is my hub code:
[Authorize]
public class MBHub : Hub
{
private readonly ILifetimeScope _hubLifetimeScope;
private readonly IUserService _userService;
public MBHub(ILifetimeScope lifetimeScope)
{
_hubLifetimeScope = lifetimeScope.BeginLifetimeScope("AutofacWebRequest");
_userService = _hubLifetimeScope.Resolve<IUserService>();
}
public override Task OnConnected()
{
System.Web.HttpContextBase httpContext = Context.Request.GetHttpContext();
var idn = (MVCSample.Biz.Profile.MyCustomIdentity)httpContext.User.Identity;
string userName = idn.Name;
string city = idn.City;
string connectionId = Context.ConnectionId;
_userService.AddConnection(connectionId, userName, city, Context.Request.Headers["User-Agent"]);
return base.OnConnected();
}
protected override void Dispose(bool disposing)
{
if (disposing && _hubLifetimeScope != null)
_hubLifetimeScope.Dispose();
base.Dispose(disposing);
}
}
source
share