【发布时间】:2016-02-28 16:29:06
【问题描述】:
我有一个 MVC 应用程序,它使用自定义主体进行基于表单的身份验证。在使用应用程序用户之前必须登录。之后我想使用 SignalR,问题是 Context.User.Identity.Name 总是空字符串。
CustomPrincipal.cs
public class CustomPrincipal : IPrincipal
{
public CustomPrincipal(IIdentity identity)
{
Identity = identity;
}
public IIdentity Identity { get; }
public bool IsInRole(string role)
{
return true;
}
}
CustomIdentity.cs
public class CustomIdentity : IIdentity
{
public CustomIdentity(EmployeeModel user)
{
Name = user.Username;
Id = user.Id;
}
public string AuthenticationType => "Custom";
public bool IsAuthenticated => !string.IsNullOrEmpty(Name);
public int Id { get; set; }
public string Name { get; }
}
BaseController.cs(我从中派生出我所有的 MVC 控制器)
protected override void OnAuthorization(AuthorizationContext context)
{
if (SessionPersister.User != null && !string.IsNullOrEmpty(SessionPersister.User.Username))
{
context.HttpContext.User = new CustomPrincipal(new CustomIdentity(SessionPersister.User));
}
base.OnAuthorization(context);
}
这里的SessionPersister只是一个存储登录用户的静态类
所以,我的 MVC 应用程序中的一切都运行良好。当用户登录并且我想向通过 SignalR 登录的另一个用户发送消息时,Identity.User.Name 在我的 Hub 类中是一个空字符串的问题:
public override Task OnConnected()
{
string name = Context.User.Identity.Name; // it's empty
return base.OnConnected();
}
有什么方法可以将我的 MVC IPrincipal 传递给 SignalR 或将其配置为使用我在 MVC 中使用的自定义身份验证?
提前感谢
【问题讨论】:
-
什么版本的 ASP.NET MVC?
-
我也遇到了同样的问题,有更新吗?
标签: authentication model-view-controller signalr identity iprincipal