我试图在SignalR Hub的OnConnected方法中使用我的自定义原则。我尝试了以下方法:
但没有运气..
它不断抛出错误:
Unable to cast object of type 'System.Web.Security.RolePrincipal' to type 'MVCSample.Biz.Profile.MyCustomPrincipal'.
在SignalR集线器中是否无法访问自定义原则?
谢谢!
这是我的中心代码:
[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()
{
//var idn = (MVCSample.Biz.Profile.MyCustomIdentity)Thread.CurrentPrincipal; <--- THIS DID NOT WORK
//var idn = (MVCSample.Biz.Profile.MyCustomIdentity)HttpContext.Current.User; <--- THIS DID NOT WORK
System.Web.HttpContextBase httpContext = Context.Request.GetHttpContext();
var idn = (MVCSample.Biz.Profile.MyCustomIdentity)httpContext.User.Identity; // <--- THIS IS MY FINAL TRY, DID NOT WORK
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)
{
// Dipose the hub lifetime scope when the hub is disposed.
if (disposing && _hubLifetimeScope != null)
_hubLifetimeScope.Dispose();
base.Dispose(disposing);
}
}
答案 0 :(得分:0)
您应该构建自己的授权属性
With ws2.Range("N" & Found.Row)
.Value = .Value + ws1.Cells(rw, "J").Value
End with
然后将此属性应用于您的Hub。
如果您想了解更多相关信息,我在博文中提到了here以及更多代码示例