C# 信号集线器中的定制原则
我试图在我的信号集线器的OnConnected方法中使用我的自定义原则。我尝试了以下方法:C# 信号集线器中的定制原则,c#,asp.net-mvc,signalr,identity,C#,Asp.net Mvc,Signalr,Identity,我试图在我的信号集线器的OnConnected方法中使用我的自定义原则。我尝试了以下方法: Context.Request.GetHttpContext().User HttpContext.Current.User Thread.CurrentPrincipal 但是没有运气 它不断抛出错误: Unable to cast object of type 'System.Web.Security.RolePrincipal' to type 'MVCSample.Biz.Profile.My
- Context.Request.GetHttpContext().User
- HttpContext.Current.User
- Thread.CurrentPrincipal
Unable to cast object of type 'System.Web.Security.RolePrincipal' to type 'MVCSample.Biz.Profile.MyCustomPrincipal'.
信号机集线器中是否没有自定义原则
谢谢大家!
这是我的中心代码:
[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);
}
}
[授权]
公共类MBHub:Hub
{
专用只读ILifetimeScope\u hubLifetimeScope;
专用只读IUserService\u userService;
公共MBHub(ILifetimeScope生存时间范围)
{
_hubLifetimeScope=lifetimeScope.BeginLifetimeScope(“AutofacWebRequest”);
_userService=\u hubLifetimeScope.Resolve();
}
已连接的公用覆盖任务()
{
//var idn=(MVCSample.Biz.Profile.MyCustomIdentity)Thread.CurrentPrincipal;您应该构建自己的Authorize属性
public class MyAuthorizeAttribute : AuthorizeAttribute
{
public override bool AuthorizeHubConnection(HubDescriptor hubDescriptor, IRequest request)
{
//put our custom user-principal into a magic "server.User" Owin variable
request.Environment["server.User"] = new MyCustomPrincipal(); //<!-THIS!
return base.AuthorizeHubConnection(hubDescriptor, request);
}
}
公共类MyAuthorizeAttribute:AuthorizeAttribute
{
公共覆盖bool AuthorizeHubConnection(HubDescriptor HubDescriptor,IRequest请求)
{
//将自定义用户主体放入神奇的“server.user”Owin变量中
request.Environment[“server.User”]=新的MyCustomPrincipal()//
然后将此属性应用于中心
如果您想了解更多信息,我在博客中提供了更多的代码示例var myCustomPrincipal=Context.User
?您能分享到目前为止您尝试过的代码吗?请分享您的自定义身份代码,即MVCSample.Biz.Profile.MyCustomIdentity
@HCJ它是一个简单的自定义身份类,用于存储其他信息用户。(即firstname、lastname、city)我想添加它的代码,但我不想让问题代码太重。它在控制器和视图上工作正常,只是在这个中心失败。AuthorizeHubMethodInvocation()
从未为我调用过,只是在AuthorizeHubConnection()中设置了主体
似乎还不够。有什么想法吗?