我正在开发一个mvc项目。我正在尝试为实时通知添加信号器。但是,通常为了安全起见,我使用会话来存储登录用户的ID,但是Signalr不允许使用会话。我进行了一些研究,发现每个人都使用表单身份验证并使用“ Context.User.Identity.Name”映射其用户。
public class ChatHub : Hub {
private static readonly ConcurrentDictionary<string, User> Users
= new ConcurrentDictionary<string, User>();
public override Task OnConnected() {
string userName = Context.User.Identity.Name;
string connectionId = Context.ConnectionId;
var user = Users.GetOrAdd(userName, _ => new User {
Name = userName,
ConnectionIds = new HashSet<string>()
});
lock (user.ConnectionIds) {
user.ConnectionIds.Add(connectionId);
// TODO: Broadcast the connected user
}
return base.OnConnected();
}
}
,它们在控制器中使用Cookie“ FormsAuthentication.SetAuthCookie(loginModel.Name,true);”填充此“ Context.User.Identity.Name”;
[HttpPost]
[ActionName("Login")]
public ActionResult PostLogin(LoginModel loginModel) {
if (ModelState.IsValid) {
FormsAuthentication.SetAuthCookie(loginModel.Name, true);
return RedirectToAction("index", "home");
}
return View(loginModel);
}
我不明白的是: “ Context.User.Identity.Name”是否存储在服务器或客户端的计算机中? 如果这存储了客户的计算机,这不是安全问题吗? 因为客户端可以更改名称并获取其他消息。
解决方案是什么?